json-artisan 0.1.2 → 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 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.
@@ -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
- clone[key] = _set(target[key], source[key]);
10
+ if (key === '__proto__')
11
+ continue;
12
+
13
+ setOwn(source, key, _set(target[key], getOwn(source, key)));
11
14
  }
12
- return clone;
15
+ return source;
13
16
  }
14
17
 
15
18
  const Set = (obj = {}) => value => {
@@ -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
- target[key] = _extend(target[key], source[key]);
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 };
@@ -3,15 +3,25 @@
3
3
  const whatIsIt = (thing) => {
4
4
  if (thing === undefined) return 'undefined';
5
5
  if (thing === null) return 'null';
6
- switch (thing.constructor) {
7
- case String: return 'string';
8
- case Number: return 'number';
9
- case Boolean: return 'boolean';
10
- case Array: return 'array';
11
- case Function: return 'function';
12
- case Object: return 'object';
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
- throw new Error('Invalid type provided to parser: ' + typeof thing);
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.2",
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": "mocha --recursive ./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
  }