"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/jsonpointer/jsonpointer.js" (7 Feb 2017, 2268 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var hasExcape = /~/
    2 var escapeMatcher = /~[01]/g
    3 function escapeReplacer (m) {
    4   switch (m) {
    5     case '~1': return '/'
    6     case '~0': return '~'
    7   }
    8   throw new Error('Invalid tilde escape: ' + m)
    9 }
   10 
   11 function untilde (str) {
   12   if (!hasExcape.test(str)) return str
   13   return str.replace(escapeMatcher, escapeReplacer)
   14 }
   15 
   16 function setter (obj, pointer, value) {
   17   var part
   18   var hasNextPart
   19 
   20   for (var p = 1, len = pointer.length; p < len;) {
   21     part = untilde(pointer[p++])
   22     hasNextPart = len > p
   23 
   24     if (typeof obj[part] === 'undefined') {
   25       // support setting of /-
   26       if (Array.isArray(obj) && part === '-') {
   27         part = obj.length
   28       }
   29 
   30       // support nested objects/array when setting values
   31       if (hasNextPart) {
   32         if ((pointer[p] !== '' && pointer[p] < Infinity) || pointer[p] === '-') obj[part] = []
   33         else obj[part] = {}
   34       }
   35     }
   36 
   37     if (!hasNextPart) break
   38     obj = obj[part]
   39   }
   40 
   41   var oldValue = obj[part]
   42   if (value === undefined) delete obj[part]
   43   else obj[part] = value
   44   return oldValue
   45 }
   46 
   47 function compilePointer (pointer) {
   48   if (typeof pointer === 'string') {
   49     pointer = pointer.split('/')
   50     if (pointer[0] === '') return pointer
   51     throw new Error('Invalid JSON pointer.')
   52   } else if (Array.isArray(pointer)) {
   53     return pointer
   54   }
   55 
   56   throw new Error('Invalid JSON pointer.')
   57 }
   58 
   59 function get (obj, pointer) {
   60   if (typeof obj !== 'object') throw new Error('Invalid input object.')
   61   pointer = compilePointer(pointer)
   62   var len = pointer.length
   63   if (len === 1) return obj
   64 
   65   for (var p = 1; p < len;) {
   66     obj = obj[untilde(pointer[p++])]
   67     if (len === p) return obj
   68     if (typeof obj !== 'object') return undefined
   69   }
   70 }
   71 
   72 function set (obj, pointer, value) {
   73   if (typeof obj !== 'object') throw new Error('Invalid input object.')
   74   pointer = compilePointer(pointer)
   75   if (pointer.length === 0) throw new Error('Invalid JSON pointer for set.')
   76   return setter(obj, pointer, value)
   77 }
   78 
   79 function compile (pointer) {
   80   var compiled = compilePointer(pointer)
   81   return {
   82     get: function (object) {
   83       return get(object, compiled)
   84     },
   85     set: function (object, value) {
   86       return set(object, compiled, value)
   87     }
   88   }
   89 }
   90 
   91 exports.get = get
   92 exports.set = set
   93 exports.compile = compile