diff --git a/example.js b/example.js index 922d8b9..ed93c23 100644 --- a/example.js +++ b/example.js @@ -4,7 +4,7 @@ var object = { a: { b: { c: 1, - d: [1,2,3], + d: [1, 2, 3], e: 'remy' } } diff --git a/lib/undefsafe.js b/lib/undefsafe.js index 0cfb5fd..60663b0 100644 --- a/lib/undefsafe.js +++ b/lib/undefsafe.js @@ -1,7 +1,6 @@ 'use strict'; function undefsafe(obj, path, value, __res) { - // I'm not super keen on this private function, but it's because // it'll also be use in the browser and I wont *one* function exposed function split(path) { @@ -19,7 +18,8 @@ function undefsafe(obj, path, value, __res) { c = path.substr(i, 1); } - if (key) { // the first value could be a string + if (key) { + // the first value could be a string res.push(key); } key = ''; @@ -51,9 +51,12 @@ function undefsafe(obj, path, value, __res) { var root = obj; var parent = obj; - var star = parts.filter(function (_) { return _ === '*' }).length > 0; + var star = + parts.filter(function(_) { + return _ === '*'; + }).length > 0; - // we're dealing with a primative + // we're dealing with a primitive if (type !== 'object' && type !== 'function') { return obj; } else if (path.trim() === '') { @@ -72,9 +75,14 @@ function undefsafe(obj, path, value, __res) { var res = __res || []; for (prop in parent) { - var shallowObj = undefsafe(obj[prop], parts.slice(i + 1).join('.'), value, res); + var shallowObj = undefsafe( + obj[prop], + parts.slice(i + 1).join('.'), + value, + res + ); if (shallowObj && shallowObj !== res) { - if ((value && shallowObj === value) || (value === undefined)) { + if ((value && shallowObj === value) || value === undefined) { if (value !== undefined) { return shallowObj; } diff --git a/package.json b/package.json index cde1d7e..1b4075e 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,10 @@ "cover": "tap test/*.test.js --cov --coverage-report=lcov", "semantic-release": "semantic-release pre && npm publish && semantic-release post" }, + "prettier": { + "trailingComma": "none", + "singleQuote": true + }, "repository": { "type": "git", "url": "https://github.com/remy/undefsafe.git" diff --git a/test/array.test.js b/test/array.test.js index 5abed19..bfc4795 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -2,9 +2,9 @@ var test = require('tap').test; var undefsafe = require('../lib/undefsafe'); -test('get specific array index', function (t) { +test('get specific array index', function(t) { var fixture = { - a: [1,2,3,4] + a: [1, 2, 3, 4] }; var res = undefsafe(fixture, 'a.2'); @@ -12,13 +12,12 @@ test('get specific array index', function (t) { t.end(); }); -test('set specific array index', function (t) { +test('set specific array index', function(t) { var fixture = { - a: [1,2,3,4] + a: [1, 2, 3, 4] }; undefsafe(fixture, 'a.2', 30); - t.deepEqual(fixture, { a: [1,2,30,4] }); + t.deepEqual(fixture, { a: [1, 2, 30, 4] }); t.end(); }); - diff --git a/test/set.test.js b/test/set.test.js index 893ac21..1e05b2e 100644 --- a/test/set.test.js +++ b/test/set.test.js @@ -2,7 +2,7 @@ var test = require('tap').test; var undefsafe = require('../lib/undefsafe'); -test('setting deep object values', function (t) { +test('setting deep object values', function(t) { var fixture = { a: { b: { @@ -14,11 +14,11 @@ test('setting deep object values', function (t) { }; undefsafe(fixture, 'a.b.c.d', 20); - t.equal(fixture.a.b.c.d, 20, 'deep primative changed'); + t.equal(fixture.a.b.c.d, 20, 'deep primitive changed'); t.end(); }); -test('setting shallow object values', function (t) { +test('setting shallow object values', function(t) { var fixture = { a: { b: { @@ -34,7 +34,7 @@ test('setting shallow object values', function (t) { t.end(); }); -test('undef value', function (t) { +test('undef value', function(t) { var fixture = { a: { b: { @@ -50,7 +50,7 @@ test('undef value', function (t) { t.end(); }); -test('missing value', function (t) { +test('missing value', function(t) { var fixture = { a: { b: { diff --git a/test/star-rule.test.js b/test/star-rule.test.js index a485a0c..ce67f0b 100644 --- a/test/star-rule.test.js +++ b/test/star-rule.test.js @@ -2,62 +2,53 @@ var test = require('tap-only'); var undefsafe = require('../lib/undefsafe'); var fixture = { - "commits": [ + commits: [ { - "modified": [ - "one", - "two" - ] + modified: ['one', 'two'] }, { - "modified": [ - "two", - "four" - ] + modified: ['two', 'four'] } ] }; -test('2.0.0: match all.*', function (t) { +test('2.0.0: match all.*', function(t) { var res = undefsafe(fixture, '*.*.*.1'); t.deepEqual(res, ['two', 'four']); t.end(); }); - -test('2.0.0: match all.*', function (t) { +test('2.0.0: match all.*', function(t) { var res = undefsafe(fixture, 'commits.*.modified.*.b'); t.deepEqual(res, ['one', 'two', 'two', 'four']); t.end(); }); - -test('get value on first * selector', function (t) { +test('get value on first * selector', function(t) { var res = undefsafe(fixture, 'commits.*.modified.0'); t.deepEqual(res, ['one', 'two']); t.end(); }); -test('walking multiple routes', function (t) { +test('walking multiple routes', function(t) { var res = undefsafe(fixture, 'commits.*.modified.*', 'four'); t.equal(res, 'four'); t.end(); }); - -test('get specific match * selector', function (t) { +test('get specific match * selector', function(t) { var res = undefsafe(fixture, 'commits.*.modified.*', 'two'); t.equal(res, 'two'); t.end(); }); -test('match * selector returns undefined', function (t) { +test('match * selector returns undefined', function(t) { var res = undefsafe(fixture, 'commits.*.modified.*', 'three'); t.equal(res, undefined); t.end(); }); -test('match * selector works on objects', function (t) { +test('match * selector works on objects', function(t) { var res = undefsafe(fixture, '*.*.modified.*', 'one'); t.equal(res, 'one'); t.end(); diff --git a/test/undefsafe.test.js b/test/undefsafe.test.js index 49f29b0..be74c90 100644 --- a/test/undefsafe.test.js +++ b/test/undefsafe.test.js @@ -2,19 +2,19 @@ var test = require('tap-only'); var undefsafe = require('../lib/undefsafe'); -test('should handle primatives', function (t) { +test('should handle primatives', function(t) { var r = undefsafe(1, ''); t.equal(r, 1, 'undefsafe is 1: ' + r); t.end(); }); -test('should handle null', function (t) { +test('should handle null', function(t) { var r = undefsafe(null, 'foo'); t.equal(r, undefined, 'undefsafe works with null'); t.end(); }); -test('should handle empty objects', function (t) { +test('should handle empty objects', function(t) { var value = {}; var r; @@ -30,11 +30,11 @@ test('should handle empty objects', function (t) { t.end(); }); -test('should handle null properties', function (t) { +test('should handle null properties', function(t) { var value = { a: { - b: null, - }, + b: null + } }; var r; @@ -47,7 +47,7 @@ test('should handle null properties', function (t) { t.end(); }); -test('should find properties with periods in them', function (t) { +test('should find properties with periods in them', function(t) { var value = { a: { 'one.two': true } }; @@ -63,10 +63,7 @@ test('should find properties with periods in them', function (t) { t.equal(r, true, 'weird: ' + r); value = { - a: { 'one.two.and\three': [ - false, - true, - ] } + a: { 'one.two.and\three': [false, true] } }; r = undefsafe(value, `a['one.two.and\three'].1`); @@ -82,23 +79,22 @@ test('should find properties with periods in them', function (t) { t.end(); }); - -test('should find deep object properties', function (t) { +test('should find deep object properties', function(t) { var value = { a: { b: { c: { d: 10, e: { - f: 20, + f: 20 }, g: true, h: false, i: undefined, - j: null, - }, - }, - }, + j: null + } + } + } }; var r;