json-artisan 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/lib/extensions/object.js +6 -3
- package/lib/json-artisan.js +5 -1
- package/lib/util/own-property.js +20 -0
- package/lib/util/what-is-it.js +18 -8
- package/package.json +2 -6
package/README.md
CHANGED
|
@@ -240,4 +240,6 @@ artisan({
|
|
|
240
240
|
|
|
241
241
|
### Contribute
|
|
242
242
|
|
|
243
|
+
Run the test suite with `npm test` on Node.js 20 or later. The tests use Node's built-in test runner.
|
|
244
|
+
|
|
243
245
|
If you like what you see please feel encouraged to [get involved](https://github.com/Kequc/json-artisan/issues) report problems and submit pull requests! As of the time of this writing the project is new with one maintainer.
|
package/lib/extensions/object.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
const whatIsIt = require('../util/what-is-it');
|
|
3
|
+
const { getOwn, setOwn } = require('../util/own-property');
|
|
3
4
|
|
|
4
5
|
const _isObject = value => whatIsIt(value) === 'object';
|
|
5
6
|
|
|
6
7
|
function _set(target, source) {
|
|
7
8
|
if (!_isObject(target) || !_isObject(source)) return source;
|
|
8
|
-
const clone = Object.assign(source);
|
|
9
9
|
for (const key of Object.keys(target)) {
|
|
10
|
-
|
|
10
|
+
if (key === '__proto__')
|
|
11
|
+
continue;
|
|
12
|
+
|
|
13
|
+
setOwn(source, key, _set(target[key], getOwn(source, key)));
|
|
11
14
|
}
|
|
12
|
-
return
|
|
15
|
+
return source;
|
|
13
16
|
}
|
|
14
17
|
|
|
15
18
|
const Set = (obj = {}) => value => {
|
package/lib/json-artisan.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
const whatIsIt = require('./util/what-is-it');
|
|
3
|
+
const { getOwn, setOwn } = require('./util/own-property');
|
|
3
4
|
|
|
4
5
|
function _extend (targetVal, sourceVal) {
|
|
5
6
|
switch (whatIsIt(sourceVal)) {
|
|
@@ -19,7 +20,10 @@ const artisan = (target, ...sources) => {
|
|
|
19
20
|
continue;
|
|
20
21
|
|
|
21
22
|
for (const key of Object.keys(source)) {
|
|
22
|
-
|
|
23
|
+
if (key === '__proto__')
|
|
24
|
+
continue;
|
|
25
|
+
|
|
26
|
+
setOwn(target, key, _extend(getOwn(target, key), source[key]));
|
|
23
27
|
if (target[key] === undefined)
|
|
24
28
|
delete target[key];
|
|
25
29
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
|
|
4
|
+
|
|
5
|
+
const getOwn = (object, key) => hasOwn(object, key) ? object[key] : undefined;
|
|
6
|
+
|
|
7
|
+
const setOwn = (object, key, value) => {
|
|
8
|
+
if (hasOwn(object, key)) {
|
|
9
|
+
object[key] = value;
|
|
10
|
+
} else {
|
|
11
|
+
Object.defineProperty(object, key, {
|
|
12
|
+
value,
|
|
13
|
+
writable: true,
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports = { getOwn, setOwn };
|
package/lib/util/what-is-it.js
CHANGED
|
@@ -3,15 +3,25 @@
|
|
|
3
3
|
const whatIsIt = (thing) => {
|
|
4
4
|
if (thing === undefined) return 'undefined';
|
|
5
5
|
if (thing === null) return 'null';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
// Look for a type's actual prototype without reading data properties on the value.
|
|
7
|
+
let prototype = Object.getPrototypeOf(thing);
|
|
8
|
+
while (prototype !== null) {
|
|
9
|
+
const descriptor = Object.getOwnPropertyDescriptor(prototype, 'constructor');
|
|
10
|
+
const constructor = descriptor && descriptor.value;
|
|
11
|
+
if (typeof constructor === 'function' && constructor.prototype === prototype) {
|
|
12
|
+
switch (constructor) {
|
|
13
|
+
case String: return 'string';
|
|
14
|
+
case Number: return 'number';
|
|
15
|
+
case Boolean: return 'boolean';
|
|
16
|
+
case Array: return 'array';
|
|
17
|
+
case Function: return 'function';
|
|
18
|
+
case Object: return 'object';
|
|
19
|
+
default: return 'unknown';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
13
23
|
}
|
|
14
|
-
return 'unknown';
|
|
24
|
+
return typeof thing === 'object' ? 'object' : 'unknown';
|
|
15
25
|
};
|
|
16
26
|
|
|
17
27
|
module.exports = whatIsIt;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-artisan",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "A JSON deep extend library",
|
|
5
5
|
"author": "mr.kequc@gmail.com",
|
|
6
6
|
"license": "ISC",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "
|
|
8
|
+
"test": "node --test"
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
11
|
"lib/"
|
|
@@ -25,9 +25,5 @@
|
|
|
25
25
|
"extend",
|
|
26
26
|
"couchdb"
|
|
27
27
|
],
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"chai": "^3.5.0",
|
|
30
|
-
"mocha": "^2.4.5"
|
|
31
|
-
},
|
|
32
28
|
"dependencies": {}
|
|
33
29
|
}
|