data-store
Advanced tools
Comparing version 0.8.2 to 0.9.0
105
index.js
@@ -10,26 +10,5 @@ 'use strict'; | ||
var Emitter = require('component-emitter'); | ||
var lazy = require('lazy-cache')(require); | ||
var utils = require('./utils'); | ||
/** | ||
* Lazily required modules. | ||
* | ||
* These modules use lazy-caching, which means that they are only | ||
* required/loaded if the method using the module is called. As a | ||
* result, data-store loads much faster. | ||
*/ | ||
lazy('collection-visit', 'visit'); | ||
lazy('extend-shallow', 'extend'); | ||
lazy('get-value', 'get'); | ||
lazy('graceful-fs', 'fs'); | ||
lazy('has-own-deep', 'hasOwn'); | ||
lazy('has-value', 'has'); | ||
lazy('kind-of', 'typeOf'); | ||
lazy('mkdirp', 'mkdirp'); | ||
lazy('object.omit', 'omit'); | ||
lazy('rimraf', 'del'); | ||
lazy('set-value', 'set'); | ||
lazy('union-value', 'union'); | ||
/** | ||
* Expose `Store` | ||
@@ -45,6 +24,8 @@ */ | ||
* ```js | ||
* var store = new Store('abc'); | ||
* var store = require('data-store')('abc'); | ||
* //=> '~/data-store/a.json' | ||
* | ||
* var store = new Store('abc', {cwd: 'test/fixtures'}); | ||
* var store = require('data-store')('abc', { | ||
* cwd: 'test/fixtures' | ||
* }); | ||
* //=> './test/fixtures/abc.json' | ||
@@ -64,8 +45,10 @@ * ``` | ||
} | ||
if (!(this instanceof Store)) { | ||
return new Store(name, options); | ||
} | ||
Emitter.call(this); | ||
options = options || {}; | ||
var cwd = options.cwd || home('data-store'); | ||
this.options = options || {}; | ||
var cwd = this.options.cwd || home('data-store'); | ||
this.indent = options.indent; | ||
this.name = name; | ||
@@ -115,8 +98,8 @@ this.path = path.join(cwd, name + '.json'); | ||
} else if (lazy.typeOf(val) === 'object') { | ||
} else if (utils.typeOf(val) === 'object') { | ||
var existing = this.get(key); | ||
val = lazy.extend({}, existing, val); | ||
val = utils.extend({}, existing, val); | ||
} | ||
lazy.set(this.data, key, val); | ||
utils.set(this.data, key, val); | ||
this.emit('set', key, val); | ||
@@ -145,5 +128,4 @@ | ||
Store.prototype.union = function (key, val) { | ||
lazy.union(this.data, key, val); | ||
utils.union(this.data, key, val); | ||
this.emit('union', key, val); | ||
this.save(); | ||
@@ -172,3 +154,3 @@ return this; | ||
Store.prototype.get = function (key) { | ||
return key ? lazy.get(this.data, key) : { | ||
return key ? utils.get(this.data, key) : { | ||
name: this.name, | ||
@@ -196,3 +178,3 @@ data: this.data | ||
Store.prototype.has = function(key) { | ||
return lazy.has(this.data, key); | ||
return utils.has(this.data, key); | ||
}; | ||
@@ -225,3 +207,3 @@ | ||
} | ||
return lazy.hasOwn(this.data, key); | ||
return utils.hasOwn(this.data, key); | ||
}; | ||
@@ -240,3 +222,3 @@ | ||
Store.prototype.save = function(dest) { | ||
writeJson(dest || this.path, this.data, this.indent); | ||
writeJson(dest || this.path, this.data, this.options.indent); | ||
}; | ||
@@ -271,3 +253,3 @@ | ||
if (keys.length) { | ||
this.data = lazy.omit(this.data, keys); | ||
this.data = utils.omit(this.data, keys); | ||
this.save(); | ||
@@ -280,3 +262,3 @@ return this; | ||
// if no keys are passed, delete the entire store | ||
lazy.del(this.path, options, function (err) { | ||
utils.del(this.path, options, function (err) { | ||
if (err) return console.error(err); | ||
@@ -297,3 +279,3 @@ this.data = {}; | ||
Store.prototype.visit = function(method, object) { | ||
lazy.visit(this, method, object); | ||
utils.visit(this, method, object); | ||
return this; | ||
@@ -303,2 +285,39 @@ }; | ||
/** | ||
* Static method for inheriting prototype and | ||
* static methods from `Store`. | ||
* | ||
* ```js | ||
* function MyApp(options) { | ||
* Store.call(this, options); | ||
* } | ||
* Store.extend(MyApp); | ||
* | ||
* // Optionally pass another object to extend onto `MyApp` | ||
* function MyApp(options) { | ||
* Store.call(this, options); | ||
* Foo.call(this, options); | ||
* } | ||
* Store.extend(MyApp, Foo.prototype); | ||
* ``` | ||
* | ||
* @param {Function} `Ctor` The constructor to extend. | ||
* @api public | ||
*/ | ||
Store.extend = function (Ctor, proto) { | ||
util.inherits(Ctor, Store); | ||
for (var key in Store) { | ||
Ctor[key] = Store[key]; | ||
} | ||
if (typeof proto === 'object') { | ||
var obj = Object.create(proto); | ||
for (var k in obj) { | ||
Ctor.prototype[k] = obj[k]; | ||
} | ||
} | ||
}; | ||
/** | ||
* Get the user's home directory | ||
@@ -326,3 +345,3 @@ * | ||
try { | ||
var str = lazy.fs.readFileSync(path.resolve(fp), 'utf8'); | ||
var str = utils.fs.readFileSync(path.resolve(fp), 'utf8'); | ||
return JSON.parse(str); | ||
@@ -348,6 +367,6 @@ } catch(err) {} | ||
try { | ||
if (!lazy.fs.existsSync(dir)) { | ||
lazy.mkdirp.sync(dir); | ||
if (!utils.fs.existsSync(dir)) { | ||
utils.mkdirp.sync(dir); | ||
} | ||
lazy.fs.writeFileSync(dest, JSON.stringify(str, null, indent)); | ||
utils.fs.writeFileSync(dest, JSON.stringify(str, null, indent)); | ||
} catch (err) { | ||
@@ -354,0 +373,0 @@ err.origin = __filename; |
{ | ||
"name": "data-store", | ||
"description": "Easily get, set and persist config data.", | ||
"version": "0.8.2", | ||
"version": "0.9.0", | ||
"homepage": "https://github.com/jonschlinkert/data-store", | ||
@@ -13,3 +13,4 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"utils.js" | ||
], | ||
@@ -24,3 +25,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"collection-visit": "^0.1.1", | ||
"collection-visit": "^0.2.0", | ||
"component-emitter": "^1.2.0", | ||
@@ -32,7 +33,7 @@ "extend-shallow": "^2.0.1", | ||
"has-value": "^0.2.0", | ||
"kind-of": "^2.0.0", | ||
"lazy-cache": "^0.2.2", | ||
"kind-of": "^2.0.1", | ||
"lazy-cache": "^0.2.3", | ||
"mkdirp": "^0.5.1", | ||
"object.omit": "^2.0.0", | ||
"rimraf": "^2.4.2", | ||
"rimraf": "^2.4.3", | ||
"set-value": "^0.2.0", | ||
@@ -39,0 +40,0 @@ "union-value": "^0.1.1" |
@@ -16,19 +16,18 @@ # data-store [![Build Status](https://travis-ci.org/jonschlinkert/data-store.svg)](https://travis-ci.org/jonschlinkert/data-store) [![NPM version](https://badge.fury.io/js/data-store.svg)](http://badge.fury.io/js/data-store) | ||
```js | ||
var Store = require('data-store'); | ||
// default cwd is `~/data-store/` | ||
var store = new Store('app', {cwd: 'actual'}); | ||
var store = require('data-store')('app', {cwd: 'actual'}); | ||
store | ||
.set('a', 'b') | ||
.set('c.d', {e: 'f'}) | ||
.set('c.d', {g: 'h'}); | ||
.set({c: 'd'}) | ||
.set('e.f', 'g') | ||
console.log(store.get('c.d')); | ||
//=> { e: 'f', g: 'h' } | ||
console.log(store.get('e.f')); | ||
//=> 'g' | ||
console.log(store.get()); | ||
//=> { name: 'app', data: { a: 'b', c: { d: { e: 'f', g: 'h' } } } } | ||
//=> {name: 'app', data: {a: 'b', c: 'd', e: {f: 'g' }}} | ||
console.log(store.data); | ||
//=> { a: 'b', c: { d: { e: 'f', g: 'h' } } } | ||
//=> {a: 'b', c: 'd', e: {f: 'g'}} | ||
``` | ||
@@ -38,3 +37,3 @@ | ||
### [Store](index.js#L58) | ||
### [Store](index.js#L39) | ||
@@ -54,10 +53,12 @@ Initialize a new `Store` with the given `name` and `options`. | ||
```js | ||
var store = new Store('abc'); | ||
var store = require('data-store')('abc'); | ||
//=> '~/data-store/a.json' | ||
var store = new Store('abc', {cwd: 'test/fixtures'}); | ||
var store = require('data-store')('abc', { | ||
cwd: 'test/fixtures' | ||
}); | ||
//=> './test/fixtures/abc.json' | ||
``` | ||
### [.set](index.js#L104) | ||
### [.set](index.js#L87) | ||
@@ -94,3 +95,3 @@ Assign `value` to `key` and save to disk. Can be a key-value pair or an object. | ||
### [.union](index.js#L140) | ||
### [.union](index.js#L123) | ||
@@ -114,3 +115,3 @@ Add or append an array of unique values to the given `key`. | ||
### [.get](index.js#L166) | ||
### [.get](index.js#L148) | ||
@@ -135,3 +136,3 @@ Get the stored `value` of `key`, or return the entire store if no `key` is defined. | ||
### [.has](index.js#L189) | ||
### [.has](index.js#L171) | ||
@@ -155,3 +156,3 @@ Returns `true` if the specified `key` has truthy value. | ||
### [.hasOwn](index.js#L214) | ||
### [.hasOwn](index.js#L196) | ||
@@ -180,3 +181,3 @@ Returns `true` if the specified `key` exists. | ||
### [.save](index.js#L231) | ||
### [.save](index.js#L213) | ||
@@ -195,3 +196,3 @@ Persist the store to disk. | ||
### [.del](index.js#L253) | ||
### [.del](index.js#L235) | ||
@@ -216,14 +217,38 @@ Delete `keys` from the store, or delete the entire store if no keys are passed. | ||
### [.extend](index.js#L294) | ||
Static method for inheriting prototype and static methods from `Store`. | ||
**Params** | ||
* `Ctor` **{Function}**: The constructor to extend. | ||
**Example** | ||
```js | ||
function MyApp(options) { | ||
Store.call(this, options); | ||
} | ||
Store.extend(MyApp); | ||
// Optionally pass another object to extend onto `MyApp` | ||
function MyApp(options) { | ||
Store.call(this, options); | ||
Foo.call(this, options); | ||
} | ||
Store.extend(MyApp, Foo.prototype); | ||
``` | ||
## Related | ||
* [assign-value](https://github.com/jonschlinkert/assign-value): Extend a value or deeply nested property of an object using object path notation. | ||
* [get-value](https://github.com/jonschlinkert/get-value): Use property paths (` a.b.c`) to get a nested value from an object. | ||
* [has-own-deep](https://github.com/jonschlinkert/has-own-deep): Returns true if an object has an own, nested property using dot notation paths ('a.b.c'). | ||
* [has-value](https://github.com/jonschlinkert/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://github.com/jonschlinkert/has-value) | ||
* [set-value](https://github.com/jonschlinkert/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | ||
* [union-value](https://github.com/jonschlinkert/union-value): Set an array of unique values as the property of an object. Supports setting deeply… [more](https://github.com/jonschlinkert/union-value) | ||
* [assign-value](https://www.npmjs.com/package/assign-value): Extend a value or deeply nested property of an object using object path notation. | [homepage](https://github.com/jonschlinkert/assign-value) | ||
* [get-value](https://www.npmjs.com/package/get-value): Use property paths (` a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value) | ||
* [has-own-deep](https://www.npmjs.com/package/has-own-deep): Returns true if an object has an own, nested property using dot notation paths ('a.b.c'). | [homepage](https://github.com/jonschlinkert/has-own-deep) | ||
* [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://www.npmjs.com/package/has-value) | [homepage](https://github.com/jonschlinkert/has-value) | ||
* [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value) | ||
* [union-value](https://www.npmjs.com/package/union-value): Set an array of unique values as the property of an object. Supports setting deeply… [more](https://www.npmjs.com/package/union-value) | [homepage](https://github.com/jonschlinkert/union-value) | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/data-store/issues/new) | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/data-store/issues/new). | ||
@@ -252,2 +277,2 @@ ## Running tests | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 19, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 30, 2015._ |
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
17454
5
343
268
+ Addedcollection-visit@0.2.3(transitive)
- Removedcollection-visit@0.1.1(transitive)
- Removedobject-visit@0.1.0(transitive)
Updatedcollection-visit@^0.2.0
Updatedkind-of@^2.0.1
Updatedlazy-cache@^0.2.3
Updatedrimraf@^2.4.3