Skip to content

Commit

Permalink
chore: prettier changes
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Feb 17, 2020
1 parent e4180ba commit f495954
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 55 deletions.
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var object = {
a: {
b: {
c: 1,
d: [1,2,3],
d: [1, 2, 3],
e: 'remy'
}
}
Expand Down
20 changes: 14 additions & 6 deletions lib/undefsafe.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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() === '') {
Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 5 additions & 6 deletions test/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
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');
t.equal(res, 3);
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();
});

10 changes: 5 additions & 5 deletions test/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down
29 changes: 10 additions & 19 deletions test/star-rule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
32 changes: 14 additions & 18 deletions test/undefsafe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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 }
};
Expand All @@ -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`);
Expand All @@ -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;

Expand Down

0 comments on commit f495954

Please sign in to comment.