Comparing version 1.8.0 to 1.8.1
@@ -81,9 +81,2 @@ declare namespace onChange { | ||
ignoreUnderscores?: boolean; | ||
/** | ||
The path will be provided as an array of keys instead of a delimited string. | ||
@default false | ||
*/ | ||
pathAsArray?: boolean; | ||
} | ||
@@ -90,0 +83,0 @@ } |
60
index.js
'use strict'; | ||
const {TARGET, UNSUBSCRIBE} = require('./lib/constants'); | ||
const path = require('./lib/path'); | ||
const isArray = require('./lib/is-array'); | ||
const isSymbol = require('./lib/is-symbol'); | ||
const PATH_SEPARATOR = '.'; | ||
const TARGET = Symbol('target'); | ||
const UNSUBSCRIBE = Symbol('unsubscribe'); | ||
@@ -22,8 +21,36 @@ const isPrimitive = value => value === null || (typeof value !== 'object' && typeof value !== 'function'); | ||
const concatPath = (path, property) => { | ||
if (property && property.toString) { | ||
if (path) { | ||
path += PATH_SEPARATOR; | ||
} | ||
path += property.toString(); | ||
} | ||
return path; | ||
}; | ||
const walkPath = (path, callback) => { | ||
let index; | ||
while (path) { | ||
index = path.indexOf(PATH_SEPARATOR); | ||
if (index === -1) { | ||
index = path.length; | ||
} | ||
callback(path.slice(0, index)); | ||
path = path.slice(index + 1); | ||
} | ||
}; | ||
const shallowClone = value => { | ||
if (isArray(value)) { | ||
if (Array.isArray(value)) { | ||
return value.slice(); | ||
} | ||
return {...value}; | ||
return Object.assign({}, value); | ||
}; | ||
@@ -43,3 +70,3 @@ | ||
const handleChange = (changePath, property, previous, value) => { | ||
const handleChange = (path, property, previous, value) => { | ||
if (isUnsubscribed) { | ||
@@ -50,3 +77,3 @@ return; | ||
if (!inApply) { | ||
onChange(path.concat(changePath, property), value, previous); | ||
onChange(concatPath(path, property), value, previous); | ||
return; | ||
@@ -58,6 +85,6 @@ } | ||
if (changePath !== applyPath) { | ||
changePath = path.after(changePath, applyPath); | ||
if (path !== applyPath) { | ||
path = path.replace(applyPath, '').slice(1); | ||
path.walk(changePath, key => { | ||
walkPath(path, key => { | ||
item[key] = shallowClone(item[key]); | ||
@@ -134,3 +161,3 @@ item = item[key]; | ||
return isUnsubscribed || | ||
(options.ignoreSymbols === true && isSymbol(property)) || | ||
(options.ignoreSymbols === true && typeof property === 'symbol') || | ||
(options.ignoreUnderscores === true && property.charAt(0) === '_') || | ||
@@ -175,3 +202,3 @@ (options.ignoreKeys !== undefined && options.ignoreKeys.includes(property)); | ||
return buildProxy(value, path.concat(pathCache.get(target), property)); | ||
return buildProxy(value, concatPath(pathCache.get(target), property)); | ||
}, | ||
@@ -248,7 +275,8 @@ | ||
if (isArray(thisArg) || toString.call(thisArg) === '[object Object]') { | ||
if (Array.isArray(thisArg) || toString.call(thisArg) === '[object Object]') { | ||
applyPrevious = shallowClone(thisArg[proxyTarget]); | ||
} | ||
applyPath = path.initial(pathCache.get(target)); | ||
applyPath = pathCache.get(target); | ||
applyPath = applyPath.slice(0, Math.max(applyPath.lastIndexOf(PATH_SEPARATOR), 0)); | ||
@@ -272,3 +300,3 @@ const result = Reflect.apply(target, thisArg, argumentsList); | ||
const proxy = buildProxy(object, options.pathAsArray === true ? [] : ''); | ||
const proxy = buildProxy(object, ''); | ||
onChange = onChange.bind(proxy); | ||
@@ -275,0 +303,0 @@ |
{ | ||
"name": "on-change", | ||
"version": "1.8.0", | ||
"version": "1.8.1", | ||
"description": "Watch an object or array for changes", | ||
"license": "MIT", | ||
"repository": "sindresorhus/on-change", | ||
"funding": "https://github.com/sindresorhus/on-change?sponsor=1", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "https://sindresorhus.com" | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd", | ||
"bench": "karma start karma.bench.conf.js" | ||
"bench": "matcha bench/bench.js" | ||
}, | ||
@@ -43,6 +42,6 @@ "files": [ | ||
"display-value": "^1.6.0", | ||
"karma-webpack-bundle": "^0.1.2", | ||
"matcha": "^0.7.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.27.2" | ||
"xo": "^0.28.0" | ||
} | ||
} |
@@ -9,2 +9,3 @@ # on-change [![Build Status](https://travis-ci.org/sindresorhus/on-change.svg?branch=master)](https://travis-ci.org/sindresorhus/on-change) | ||
## Install | ||
@@ -16,2 +17,3 @@ | ||
## Usage | ||
@@ -84,2 +86,3 @@ | ||
## API | ||
@@ -116,3 +119,3 @@ | ||
Type: `boolean`\ | ||
Type: `boolean`<br> | ||
Default: `false` | ||
@@ -124,3 +127,3 @@ | ||
Type: `Function`\ | ||
Type: `Function`<br> | ||
Default: [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) | ||
@@ -132,4 +135,4 @@ | ||
Type: `boolean`\ | ||
Default: `false` | ||
Type: `boolean`<br> | ||
Default: false | ||
@@ -140,4 +143,4 @@ Setting properties as `Symbol` won't trigger the callback. | ||
Type: `Array<string | symbol>`\ | ||
Default: `undefined` | ||
Type: `Array<string|symbol>`<br> | ||
Default: undefined | ||
@@ -148,14 +151,7 @@ Setting properties in this array won't trigger the callback. | ||
Type: `boolean`\ | ||
Default: `false` | ||
Type: `boolean`<br> | ||
Default: false | ||
Setting properties with an underscore as the first character won't trigger the callback. | ||
##### pathAsArray | ||
Type: `boolean`\ | ||
Default: `false` | ||
The path will be provided as an array of keys instead of a delimited string. | ||
### onChange.target(object) | ||
@@ -181,2 +177,3 @@ | ||
## Use-case | ||
@@ -230,2 +227,3 @@ | ||
## Related | ||
@@ -238,2 +236,3 @@ | ||
## Maintainers | ||
@@ -240,0 +239,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17484
387
231