From f800ecc9446c1d4479a3405078a289378f6e561a Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Tue, 29 Sep 2015 21:52:23 +0200 Subject: [PATCH 1/2] Lint files using javascript standard style --- jsonpointer.js | 84 ++++++++++++++--------------- package.json | 7 ++- test.js | 144 ++++++++++++++++++++++++------------------------- 3 files changed, 118 insertions(+), 117 deletions(-) diff --git a/jsonpointer.js b/jsonpointer.js index 8fb3397..c0ae809 100644 --- a/jsonpointer.js +++ b/jsonpointer.js @@ -1,99 +1,97 @@ -var untilde = function(str) { - return str.replace(/~./g, function(m) { +var untilde = function (str) { + return str.replace(/~./g, function (m) { switch (m) { - case "~0": - return "~"; - case "~1": - return "/"; + case '~0': return '~' + case '~1': return '/' } - throw new Error("Invalid tilde escape: " + m); - }); + throw new Error('Invalid tilde escape: ' + m) + }) } -var traverse = function(obj, pointer, value) { - var part = untilde(pointer.shift()); - var isJustReading = arguments.length === 2; +var traverse = function (obj, pointer, value) { + var part = untilde(pointer.shift()) + var isJustReading = arguments.length === 2 if (obj[part] == null) { - if (isJustReading) return null; + if (isJustReading) return null // support setting of /- if (part === '-' && obj instanceof Array) { - part = obj.length; + part = obj.length } // support nested objects/array when setting values - var nextPart = pointer[0]; + var nextPart = pointer[0] if (nextPart === '-' || !isNaN(nextPart)) { - obj[part] = []; + obj[part] = [] } else if (nextPart) { - obj[part] = {}; + obj[part] = {} } } // keep traversing if (pointer.length !== 0) { if (isJustReading) { - return traverse(obj[part], pointer); + return traverse(obj[part], pointer) } else { - return traverse(obj[part], pointer, value); + return traverse(obj[part], pointer, value) } } // we're done if (isJustReading) { - return obj[part]; + return obj[part] } // set new value, return old value - var oldValue = obj[part]; + var oldValue = obj[part] if (value === null) { - delete obj[part]; + delete obj[part] } else { - obj[part] = value; + obj[part] = value } - return oldValue; + return oldValue } -var compilePointer = function(pointer) { - if(pointer === "") { - return []; +var compilePointer = function (pointer) { + if (pointer === '') { + return [] } - if(!pointer) { - throw new Error("Invalid JSON pointer."); + if (!pointer) { + throw new Error('Invalid JSON pointer.') } if (!(pointer instanceof Array)) { - pointer = pointer.split("/"); - if (pointer.shift() !== "") throw new Error("Invalid JSON pointer.") + pointer = pointer.split('/') + if (pointer.shift() !== '') throw new Error('Invalid JSON pointer.') } - return pointer; + return pointer } -var validate_input = function(obj, pointer) { - if(typeof obj !== "object") { - throw new Error("Invalid input object."); +var validateInput = function (obj, pointer) { + if (typeof obj !== 'object') { + throw new Error('Invalid input object.') } - return compilePointer(pointer); + return compilePointer(pointer) } -var get = function(obj, pointer) { - pointer = validate_input(obj, pointer); +var get = function (obj, pointer) { + pointer = validateInput(obj, pointer) if (pointer.length === 0) { - return obj; + return obj } - return traverse(obj, pointer); + return traverse(obj, pointer) } -var set = function(obj, pointer, value) { - pointer = validate_input(obj, pointer); +var set = function (obj, pointer, value) { + pointer = validateInput(obj, pointer) if (pointer.length === 0) { - throw new Error("Invalid JSON pointer for set.") + throw new Error('Invalid JSON pointer for set.') } - return traverse(obj, pointer, value); + return traverse(obj, pointer, value) } exports.get = get diff --git a/package.json b/package.json index 7139962..24a6c8b 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,10 @@ }, "main": "./jsonpointer", "scripts": { - "test": "node test.js" + "test": "standard && node test.js" }, - "license": "MIT" + "license": "MIT", + "devDependencies": { + "standard": "^5.3.1" + } } diff --git a/test.js b/test.js index a596843..fc48d54 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ -var assert = require('assert'); -var jsonpointer = require('./jsonpointer'); +var assert = require('assert') +var jsonpointer = require('./jsonpointer') var obj = { a: 1, @@ -7,102 +7,102 @@ var obj = { c: 2 }, d: { - e: [{a:3}, {b:4}, {c:5}] + e: [{ a: 3 }, { b: 4 }, { c: 5 }] } -}; +} -assert.equal(jsonpointer.get(obj, "/a"), 1); -assert.equal(jsonpointer.get(obj, "/b/c"), 2); -assert.equal(jsonpointer.get(obj, "/d/e/0/a"), 3); -assert.equal(jsonpointer.get(obj, "/d/e/1/b"), 4); -assert.equal(jsonpointer.get(obj, "/d/e/2/c"), 5); +assert.equal(jsonpointer.get(obj, '/a'), 1) +assert.equal(jsonpointer.get(obj, '/b/c'), 2) +assert.equal(jsonpointer.get(obj, '/d/e/0/a'), 3) +assert.equal(jsonpointer.get(obj, '/d/e/1/b'), 4) +assert.equal(jsonpointer.get(obj, '/d/e/2/c'), 5) // set returns old value -assert.equal(jsonpointer.set(obj, "/a", 2), 1); -assert.equal(jsonpointer.set(obj, "/b/c", 3), 2); -assert.equal(jsonpointer.set(obj, "/d/e/0/a", 4), 3); -assert.equal(jsonpointer.set(obj, "/d/e/1/b", 5), 4); -assert.equal(jsonpointer.set(obj, "/d/e/2/c", 6), 5); +assert.equal(jsonpointer.set(obj, '/a', 2), 1) +assert.equal(jsonpointer.set(obj, '/b/c', 3), 2) +assert.equal(jsonpointer.set(obj, '/d/e/0/a', 4), 3) +assert.equal(jsonpointer.set(obj, '/d/e/1/b', 5), 4) +assert.equal(jsonpointer.set(obj, '/d/e/2/c', 6), 5) // set nested properties -assert.equal(jsonpointer.set(obj, "/f/g/h/i", 6), undefined); -assert.equal(jsonpointer.get(obj, "/f/g/h/i"), 6); +assert.equal(jsonpointer.set(obj, '/f/g/h/i', 6), undefined) +assert.equal(jsonpointer.get(obj, '/f/g/h/i'), 6) // set an array -assert.equal(jsonpointer.set(obj, "/f/g/h/foo/-", 'test'), undefined); -arr = jsonpointer.get(obj, "/f/g/h/foo") -assert(Array.isArray(arr), 'set /- creates an array.'); -assert.equal(arr[0], 'test'); +assert.equal(jsonpointer.set(obj, '/f/g/h/foo/-', 'test'), undefined) +var arr = jsonpointer.get(obj, '/f/g/h/foo') +assert(Array.isArray(arr), 'set /- creates an array.') +assert.equal(arr[0], 'test') -assert.equal(jsonpointer.get(obj, "/a"), 2); -assert.equal(jsonpointer.get(obj, "/b/c"), 3); -assert.equal(jsonpointer.get(obj, "/d/e/0/a"), 4); -assert.equal(jsonpointer.get(obj, "/d/e/1/b"), 5); -assert.equal(jsonpointer.get(obj, "/d/e/2/c"), 6); +assert.equal(jsonpointer.get(obj, '/a'), 2) +assert.equal(jsonpointer.get(obj, '/b/c'), 3) +assert.equal(jsonpointer.get(obj, '/d/e/0/a'), 4) +assert.equal(jsonpointer.get(obj, '/d/e/1/b'), 5) +assert.equal(jsonpointer.get(obj, '/d/e/2/c'), 6) -assert.equal(jsonpointer.get(obj, ""), obj); -assert.throws(function(){ jsonpointer.get(obj, "a"); }, validateError); -assert.throws(function(){ jsonpointer.get(obj, "a/"); }, validateError); +assert.equal(jsonpointer.get(obj, ''), obj) +assert.throws(function () { jsonpointer.get(obj, 'a') }, validateError) +assert.throws(function () { jsonpointer.get(obj, 'a/') }, validateError) -function validateError(err) { - if ( (err instanceof Error) && /Invalid JSON pointer/.test(err.message) ) { - return true; +function validateError (err) { + if ((err instanceof Error) && /Invalid JSON pointer/.test(err.message)) { + return true } } var complexKeys = { - "a/b": { + 'a/b': { c: 1 }, d: { - "e/f": 2 + 'e/f': 2 }, - "~1": 3, - "01": 4 + '~1': 3, + '01': 4 } -assert.equal(jsonpointer.get(complexKeys, "/a~1b/c"), 1); -assert.equal(jsonpointer.get(complexKeys, "/d/e~1f"), 2); -assert.equal(jsonpointer.get(complexKeys, "/~01"), 3); -assert.equal(jsonpointer.get(complexKeys, "/01"), 4); -assert.equal(jsonpointer.get(complexKeys, "/a/b/c"), null); -assert.equal(jsonpointer.get(complexKeys, "/~1"), null); +assert.equal(jsonpointer.get(complexKeys, '/a~1b/c'), 1) +assert.equal(jsonpointer.get(complexKeys, '/d/e~1f'), 2) +assert.equal(jsonpointer.get(complexKeys, '/~01'), 3) +assert.equal(jsonpointer.get(complexKeys, '/01'), 4) +assert.equal(jsonpointer.get(complexKeys, '/a/b/c'), null) +assert.equal(jsonpointer.get(complexKeys, '/~1'), null) // draft-ietf-appsawg-json-pointer-08 has special array rules -var ary = [ "zero", "one", "two" ]; -assert.equal(jsonpointer.get(ary, "/01"), null); +var ary = [ 'zero', 'one', 'two' ] +assert.equal(jsonpointer.get(ary, '/01'), null) -assert.equal(jsonpointer.set(ary, "/-", "three"), null); -assert.equal(ary[3], "three"); +assert.equal(jsonpointer.set(ary, '/-', 'three'), null) +assert.equal(ary[3], 'three') // Examples from the draft: var example = { - "foo": ["bar", "baz"], - "": 0, - "a/b": 1, - "c%d": 2, - "e^f": 3, - "g|h": 4, - "i\\j": 5, - "k\"l": 6, - " ": 7, - "m~n": 8 -}; + 'foo': ['bar', 'baz'], + '': 0, + 'a/b': 1, + 'c%d': 2, + 'e^f': 3, + 'g|h': 4, + 'i\\j': 5, + 'k\'l': 6, + ' ': 7, + 'm~n': 8 +} -assert.equal(jsonpointer.get(example, ""), example); -var ans = jsonpointer.get(example, "/foo"); -assert.equal(ans.length, 2); -assert.equal(ans[0], "bar"); -assert.equal(ans[1], "baz"); -assert.equal(jsonpointer.get(example, "/foo/0"), "bar"); -assert.equal(jsonpointer.get(example, "/"), 0); -assert.equal(jsonpointer.get(example, "/a~1b"), 1); -assert.equal(jsonpointer.get(example, "/c%d"), 2); -assert.equal(jsonpointer.get(example, "/e^f"), 3); -assert.equal(jsonpointer.get(example, "/g|h"), 4); -assert.equal(jsonpointer.get(example, "/i\\j"), 5); -assert.equal(jsonpointer.get(example, "/k\"l"), 6); -assert.equal(jsonpointer.get(example, "/ "), 7); -assert.equal(jsonpointer.get(example, "/m~0n"), 8); +assert.equal(jsonpointer.get(example, ''), example) +var ans = jsonpointer.get(example, '/foo') +assert.equal(ans.length, 2) +assert.equal(ans[0], 'bar') +assert.equal(ans[1], 'baz') +assert.equal(jsonpointer.get(example, '/foo/0'), 'bar') +assert.equal(jsonpointer.get(example, '/'), 0) +assert.equal(jsonpointer.get(example, '/a~1b'), 1) +assert.equal(jsonpointer.get(example, '/c%d'), 2) +assert.equal(jsonpointer.get(example, '/e^f'), 3) +assert.equal(jsonpointer.get(example, '/g|h'), 4) +assert.equal(jsonpointer.get(example, '/i\\j'), 5) +assert.equal(jsonpointer.get(example, '/k\'l'), 6) +assert.equal(jsonpointer.get(example, '/ '), 7) +assert.equal(jsonpointer.get(example, '/m~0n'), 8) -console.log("All tests pass."); +console.log('All tests pass.') From 9a99231c8f9347b627fb92c8350eb7f6d517c452 Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Wed, 30 Sep 2015 00:03:38 +0200 Subject: [PATCH 2/2] Drop support of node v0.6.0 and v0.8.0 --- .travis.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9338bf1..7f56324 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,7 @@ language: "node_js" node_js: - - 0.6 - - 0.8 - 0.10 - 0.11 - 0.12 - - iojs-v1.0 - - iojs-v2.0 - - iojs + - 4.0 + - node