Comparing version 4.0.2 to 4.1.0
@@ -128,2 +128,40 @@ /// <reference types="node"/> | ||
readonly projectSuffix?: string; | ||
/** | ||
Access nested properties by dot notation. | ||
@default true | ||
@example | ||
``` | ||
const config = new Conf(); | ||
config.set({ | ||
foo: { | ||
bar: { | ||
foobar: 'π¦' | ||
} | ||
} | ||
}); | ||
console.log(config.get('foo.bar.foobar')); | ||
//=> 'π¦' | ||
``` | ||
Alternatively, you can set this option to `false` so the whole string would be treated as one key. | ||
@example | ||
``` | ||
const config = new Conf({accessPropertiesByDotNotation: false}); | ||
config.set({ | ||
`foo.bar.foobar`: 'π¦' | ||
}); | ||
console.log(config.get('foo.bar.foobar')); | ||
//=> 'π¦' | ||
``` | ||
*/ | ||
readonly accessPropertiesByDotNotation?: boolean; | ||
} | ||
@@ -216,2 +254,11 @@ } | ||
/** | ||
Watches the whole config object, calling `callback` on any changes. | ||
@param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`. | ||
*/ | ||
onDidAnyChange( | ||
callback: (oldValue: {[key: string]: T} | undefined, newValue: {[key: string]: T} | undefined) => void | ||
): () => void; | ||
[Symbol.iterator](): IterableIterator<[string, T]>; | ||
@@ -218,0 +265,0 @@ } |
46
index.js
@@ -45,2 +45,3 @@ /* eslint-disable node/no-deprecated-api */ | ||
deserialize: JSON.parse, | ||
accessPropertiesByDotNotation: true, | ||
...options | ||
@@ -116,3 +117,7 @@ }; | ||
get(key, defaultValue) { | ||
return dotProp.get(this.store, key, defaultValue); | ||
if (this._options.accessPropertiesByDotNotation) { | ||
return dotProp.get(this.store, key, defaultValue); | ||
} | ||
return key in this.store ? this.store[key] : defaultValue; | ||
} | ||
@@ -133,3 +138,7 @@ | ||
checkValueType(key, value); | ||
dotProp.set(store, key, value); | ||
if (this._options.accessPropertiesByDotNotation) { | ||
dotProp.set(store, key, value); | ||
} else { | ||
store[key] = value; | ||
} | ||
}; | ||
@@ -150,3 +159,7 @@ | ||
has(key) { | ||
return dotProp.has(this.store, key); | ||
if (this._options.accessPropertiesByDotNotation) { | ||
return dotProp.has(this.store, key); | ||
} | ||
return key in this.store; | ||
} | ||
@@ -156,3 +169,8 @@ | ||
const {store} = this; | ||
dotProp.delete(store, key); | ||
if (this._options.accessPropertiesByDotNotation) { | ||
dotProp.delete(store, key); | ||
} else { | ||
delete store[key]; | ||
} | ||
this.store = store; | ||
@@ -174,7 +192,23 @@ } | ||
let currentValue = this.get(key); | ||
const getter = () => this.get(key); | ||
return this.handleChange(getter, callback); | ||
} | ||
onDidAnyChange(callback) { | ||
if (typeof callback !== 'function') { | ||
throw new TypeError(`Expected \`callback\` to be of type \`function\`, got ${typeof callback}`); | ||
} | ||
const getter = () => this.store; | ||
return this.handleChange(getter, callback); | ||
} | ||
handleChange(getter, callback) { | ||
let currentValue = getter(); | ||
const onChange = () => { | ||
const oldValue = currentValue; | ||
const newValue = this.get(key); | ||
const newValue = getter(); | ||
@@ -181,0 +215,0 @@ try { |
{ | ||
"name": "conf", | ||
"version": "4.0.2", | ||
"version": "4.1.0", | ||
"description": "Simple config handling for your app or module", | ||
@@ -48,13 +48,13 @@ "license": "MIT", | ||
"pkg-up": "^3.0.1", | ||
"write-file-atomic": "^2.4.2" | ||
"write-file-atomic": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^11.13.4", | ||
"@types/node": "^12.0.4", | ||
"ava": "^1.4.1", | ||
"clear-module": "^3.2.0", | ||
"del": "^4.1.0", | ||
"tempy": "^0.2.1", | ||
"tsd": "^0.7.2", | ||
"tempy": "^0.3.0", | ||
"tsd": "^0.7.3", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -16,7 +16,3 @@ # conf [![Build Status](https://travis-ci.org/sindresorhus/conf.svg?branch=master)](https://travis-ci.org/sindresorhus/conf) | ||
<a href="https://www.patreon.com/sindresorhus"> | ||
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160"> | ||
</a> | ||
## Usage | ||
@@ -192,2 +188,37 @@ | ||
#### accessPropertiesByDotNotation | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Accessing nested properties by dot notation. For example: | ||
```js | ||
const config = new Conf(); | ||
config.set({ | ||
foo: { | ||
bar: { | ||
foobar: 'π¦' | ||
} | ||
} | ||
}); | ||
console.log(config.get('foo.bar.foobar')); | ||
//=> 'π¦' | ||
``` | ||
Alternatively, you can set this option to `false` so the whole string would be treated as one key. | ||
```js | ||
const config = new Conf({accessPropertiesByDotNotation: false}); | ||
config.set({ | ||
`foo.bar.foobar`: 'π¦' | ||
}); | ||
console.log(config.get('foo.bar.foobar')); | ||
//=> 'π¦' | ||
``` | ||
### Instance | ||
@@ -231,2 +262,8 @@ | ||
#### .onDidAnyChange(callback) | ||
`callback`: `(newValue, oldValue) => {}` | ||
Watches the whole config object, calling `callback` on any changes. `oldValue` and `newValue` will be the config object before and after the change, respectively. You must compare `oldValue` to `newValue` to find out what changed. | ||
#### .size | ||
@@ -233,0 +270,0 @@ |
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
27054
422
319
+ Addedis-typedarray@1.0.0(transitive)
+ Addedtypedarray-to-buffer@3.1.5(transitive)
+ Addedwrite-file-atomic@3.0.3(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedwrite-file-atomic@2.4.3(transitive)
Updatedwrite-file-atomic@^3.0.0