pax_global_header 0000666 0000000 0000000 00000000064 13272150524 0014513 g ustar 00root root 0000000 0000000 52 comment=666f13527fc19926d34e8db735f61fa7f4bbc4fd
node-cache-base-4.0.0/ 0000775 0000000 0000000 00000000000 13272150524 0014412 5 ustar 00root root 0000000 0000000 node-cache-base-4.0.0/.editorconfig 0000664 0000000 0000000 00000000441 13272150524 0017066 0 ustar 00root root 0000000 0000000 # http://editorconfig.org/
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[{**/{actual,fixtures,expected,templates}/**,*.md}]
trim_trailing_whitespace = false
insert_final_newline = false
node-cache-base-4.0.0/.eslintrc.json 0000664 0000000 0000000 00000007325 13272150524 0017215 0 ustar 00root root 0000000 0000000 {
"extends": [
"eslint:recommended"
],
"env": {
"browser": false,
"es6": true,
"node": true,
"mocha": true
},
"parserOptions":{
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true
}
},
"globals": {
"document": false,
"navigator": false,
"window": false
},
"rules": {
"accessor-pairs": 2,
"arrow-spacing": [2, { "before": true, "after": true }],
"block-spacing": [2, "always"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
"comma-dangle": [2, "never"],
"comma-spacing": [2, { "before": false, "after": true }],
"comma-style": [2, "last"],
"constructor-super": 2,
"curly": [2, "multi-line"],
"dot-location": [2, "property"],
"eol-last": 2,
"eqeqeq": [2, "allow-null"],
"generator-star-spacing": [2, { "before": true, "after": true }],
"handle-callback-err": [2, "^(err|error)$" ],
"indent": [2, 2, { "SwitchCase": 1 }],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"keyword-spacing": [2, { "before": true, "after": true }],
"new-cap": [2, { "newIsCap": true, "capIsNew": false }],
"new-parens": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-class-assign": 2,
"no-cond-assign": 2,
"no-const-assign": 2,
"no-control-regex": 2,
"no-debugger": 2,
"no-delete-var": 2,
"no-dupe-args": 2,
"no-dupe-class-members": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-empty-character-class": 2,
"no-eval": 2,
"no-ex-assign": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-boolean-cast": 2,
"no-extra-parens": [2, "functions"],
"no-fallthrough": 2,
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implied-eval": 2,
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-multiple-empty-lines": [2, { "max": 1 }],
"no-native-reassign": 0,
"no-negated-in-lhs": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-require": 2,
"no-new-wrappers": 2,
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-proto": 0,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-return-assign": 2,
"no-self-compare": 2,
"no-sequences": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-sparse-arrays": 2,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 0,
"no-undef": 2,
"no-undef-init": 2,
"no-unexpected-multiline": 2,
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
"no-unreachable": 2,
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
"no-useless-call": 0,
"no-with": 2,
"one-var": [0, { "initialized": "never" }],
"operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
"padded-blocks": [0, "never"],
"quotes": [2, "single", "avoid-escape"],
"radix": 2,
"semi": [2, "always"],
"semi-spacing": [2, { "before": false, "after": true }],
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, "never"],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
"use-isnan": 2,
"valid-typeof": 2,
"wrap-iife": [2, "any"],
"yoda": [2, "never"]
}
}
node-cache-base-4.0.0/.gitattributes 0000664 0000000 0000000 00000000177 13272150524 0017312 0 ustar 00root root 0000000 0000000 # Enforce Unix newlines
* text eol=lf
# binaries
*.ai binary
*.psd binary
*.jpg binary
*.gif binary
*.png binary
*.jpeg binary node-cache-base-4.0.0/.gitignore 0000664 0000000 0000000 00000000456 13272150524 0016407 0 ustar 00root root 0000000 0000000 # always ignore files
*.DS_Store
.idea
.vscode
*.sublime-*
# test related, or directories generated by tests
test/actual
actual
coverage
.nyc*
# npm
node_modules
npm-debug.log
# yarn
yarn.lock
yarn-error.log
# misc
_gh_pages
_draft
_drafts
bower_components
vendor
temp
tmp
TODO.md
package-lock.json node-cache-base-4.0.0/.npmrc 0000664 0000000 0000000 00000000023 13272150524 0015525 0 ustar 00root root 0000000 0000000 package-lock=false
node-cache-base-4.0.0/.travis.yml 0000664 0000000 0000000 00000000146 13272150524 0016524 0 ustar 00root root 0000000 0000000 sudo: false
os:
- linux
- osx
language: node_js
node_js:
- node
- '9'
- '8'
- '7'
- '6'
node-cache-base-4.0.0/.verb.md 0000664 0000000 0000000 00000003147 13272150524 0015755 0 ustar 00root root 0000000 0000000 ## Quickstart
```js
const CacheBase = require('cache-base');
const app = new CacheBase();
app.set('a.b', 'c');
console.log(app.cache.a); //=> { b: 'c' }
console.log(app.cache.a.b); //=> 'c'
console.log(app.get('a')); //=> { b: 'c' }
console.log(app.get('a.b')); //=> 'c'
```
More [usage examples](#usage-examples) below.
## API
{%= apidocs("index.js") %}
## Usage examples
**Create an instance of cache-base**
```js
const app = new CacheBase();
app.set('a', 'b');
app.set('c.d', 'e');
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app);
//=> CacheBase { a: 'b' }
```
**Initialize with an object**
```js
const app = new CacheBase({ a: 'b', c: { d: 'e' } });
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app.get('c.d'));
//=> 'e'
console.log(app);
//=> CacheBase { cache: { a: 'b' } }
```
**Inherit**
```js
class MyApp extends CacheBase {}
const app = new MyApp();
app.set('a', 'b');
app.set('c', 'd');
console.log(app.get('a'));
//=> 'b'
console.log(app);
//=> MyApp { cache: { a: 'b', c: 'd' } }
```
**Custom namespace**
Pass a string as the first value to the contructor to define a custom property name to use for the cache. By default values are stored on the `cache` property.
```js
const CacheBase = require('cache-base');
const app = new CacheBase('data', { a: 'b' });
app.set('c.d', 'e');
// get values
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app.data);
//=> { a: 'b', c: { d: 'e' } }
console.log(app);
//=> CacheBase { data: { a: 'b', c: { d: 'e' } } }
```
node-cache-base-4.0.0/CHANGELOG.md 0000664 0000000 0000000 00000007322 13272150524 0016227 0 ustar 00root root 0000000 0000000 # Release history
All notable changes to this project will be documented in this file.
This changelog's format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and versioning in this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
Guiding Principles
- Changelogs are for humans, not machines.
- There should be an entry for every single version.
- The same types of changes should be grouped.
- Versions and sections should be linkable.
- The latest version comes first.
- The release date of each versions is displayed.
- Mention whether you follow Semantic Versioning.
Types of changes
Changelog entries are classified using the following labels _(from [keep-a-changelog](http://keepachangelog.com/)_):
- `Added` for new features.
- `Changed` for changes in existing functionality.
- `Deprecated` for soon-to-be removed features.
- `Removed` for now removed features.
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.
## [3.0.0] - 2018-01-27
**Changed**
- adds `.prime()` and `.default()` methods
## [2.0.0] - 2017-12-17
**Changed**
- convert to class
- refactor to allow namespace to be set on constructor, thus `.namespace` was removed from the main export. Please see the readme for details.
## [1.0.1] - 2017-07-22
- run update, lint, update deps
## [0.8.5] - 2017-02-25
- Bump `isobject`.
- Bump `set-value`.
- Merge pull request #7 from wtgtybhertgeghgtwtg/bump-dependencies
- run udpate
## [0.8.4] - 2016-05-30
- run update
- adds `.union` method. resolves https://github.com/jonschlinkert/cache-base/issues/3
- generate docs
- run update, update deps
- make `.union` key behavior consistent with other methods
- generate docs
## [0.8.2] - 2016-03-02
- handle single arg key as an array
- generate docs
## [0.8.1] - 2016-02-29
- ensure value is a non-array object, only clear if `prop` is defined
## [0.8.0] - 2016-02-09
- run update
- run update, lint
- minor refactor
- generate docs with verb
## [0.7.1] - 2015-11-23
- update deps
- lint
- adds lazy-cache, event emitting
- events tests
## [0.7.0] - 2015-11-23
- use eslint, lint
- refactor, simplify
## [0.6.0] - 2015-03-11
- fix pick docs
- lint
- get rid of deps
## [0.4.0] - 2015-02-14
- adds `.pick()` and `.omit()` methods
- adds tests
- build docs/readme
## [0.3.0] - 2015-02-13
- adds npmignore
- adds travis
- rename
- copyright year, lint
- update deps
- build readme
## [0.2.0] - 2014-11-15
- remove namespace stuff
- update fixtures
- update verbfile
- clean up, refactor a number of methods:
- update docs, run verb
## 0.1.0
- first commit
[2.0.0]: https://github.com/jonschlinkert/cache-base/compare/1.0.1...2.0.0
[1.0.1]: https://github.com/jonschlinkert/cache-base/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/jonschlinkert/cache-base/compare/0.8.5...1.0.0
[0.8.5]: https://github.com/jonschlinkert/cache-base/compare/0.8.4...0.8.5
[0.8.4]: https://github.com/jonschlinkert/cache-base/compare/0.8.2...0.8.4
[0.8.2]: https://github.com/jonschlinkert/cache-base/compare/0.8.1...0.8.2
[0.8.1]: https://github.com/jonschlinkert/cache-base/compare/0.8.0...0.8.1
[0.8.0]: https://github.com/jonschlinkert/cache-base/compare/0.7.1...0.8.0
[0.7.1]: https://github.com/jonschlinkert/cache-base/compare/0.7.0...0.7.1
[0.7.0]: https://github.com/jonschlinkert/cache-base/compare/0.6.0...0.7.0
[0.6.0]: https://github.com/jonschlinkert/cache-base/compare/0.4.0...0.6.0
[0.4.0]: https://github.com/jonschlinkert/cache-base/compare/0.3.0...0.4.0
[0.3.0]: https://github.com/jonschlinkert/cache-base/compare/0.2.0...0.3.0
[keep-a-changelog]: https://github.com/olivierlacan/keep-a-changelog
node-cache-base-4.0.0/LICENSE 0000664 0000000 0000000 00000002100 13272150524 0015410 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2014-2018, Jon Schlinkert.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
node-cache-base-4.0.0/README.md 0000664 0000000 0000000 00000030221 13272150524 0015667 0 ustar 00root root 0000000 0000000 # cache-base [](https://www.npmjs.com/package/cache-base) [](https://npmjs.org/package/cache-base) [](https://npmjs.org/package/cache-base) [](https://travis-ci.org/jonschlinkert/cache-base)
> Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
- [Install](#install)
- [Quickstart](#quickstart)
- [API](#api)
- [Usage examples](#usage-examples)
- [About](#about)
_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install --save cache-base
```
## Quickstart
```js
const CacheBase = require('cache-base');
const app = new CacheBase();
app.set('a.b', 'c');
console.log(app.cache.a); //=> { b: 'c' }
console.log(app.cache.a.b); //=> 'c'
console.log(app.get('a')); //=> { b: 'c' }
console.log(app.get('a.b')); //=> 'c'
```
More [usage examples](#usage-examples) below.
## API
**Params**
* `prop` **{String|Object}**: (optional) Property name to use for the cache, or the object to initialize with.
* `cache` **{Object}**: (optional) An object to initialize with.
**Example**
```js
const app = new CacheBase();
```
### [.set](index.js#L65)
Assign `value` to `key`. Also emits `set` with the key and value.
**Params**
* `key` **{String|Array}**: The name of the property to set. Dot-notation may be used to set nested properties.
* `value` **{any}**
* `returns` **{Object}**: Returns the instance for chaining.
**Events**
* `emits`: `set` with `key` and `value` as arguments.
**Example**
```js
app.on('set', function(key, val) {
// do something when `set` is emitted
});
app.set('admin', true);
// also takes an object or an array of objects
app.set({ name: 'Brian' });
app.set([{ foo: 'bar' }, { baz: 'quux' }]);
console.log(app);
//=> { name: 'Brian', foo: 'bar', baz: 'quux' }
```
### [.get](index.js#L90)
Return the value of `key`.
**Params**
* `key` **{String|Array}**: The name of the property to get. Dot-notation may be used to set nested properties.
* `returns` **{any}**: Returns the value of `key`
**Events**
* `emits`: `get` with `key` and `value` as arguments.
**Example**
```js
app.set('a.b.c', 'd');
app.get('a.b');
//=> { c: 'd' }
```
### [.prime](index.js#L120)
Create a property on the cache with the given `value` only if it doesn't already exist.
**Params**
* `key` **{String}**: Property name or object path notation.
* `val` **{any}**
* `returns` **{Object}**: Returns the instance for chaining.
**Example**
```js
console.log(app.cache); //=> {}
app.set('one', { foo: 'bar' });
app.prime('one', { a: 'b' });
app.prime('two', { c: 'd' });
console.log(app.cache.one); //=> { foo: 'bar' }
console.log(app.cache.two); //=> { c: 'd' }
```
### [.default](index.js#L162)
Set a default value to be used when `.get()` is called and the value is not defined on the cache. Returns a value from the defaults when only a key is passed.
**Params**
* `key` **{String|Array}**: The name of the property to set. Dot-notation may be used to set nested properties.
* `value` **{any}**: (optional) The value to set on the defaults object.
* `returns` **{Object}**: Returns the instance for chaining.
**Example**
```js
app.set('foo', 'xxx');
app.default('foo', 'one');
app.default('bar', 'two');
app.default('baz', 'three');
app.set('baz', 'zzz');
console.log(app.get('foo'));
//=> 'xxx'
console.log(app.get('bar'));
//=> 'two'
console.log(app.get('baz'));
//=> 'zzz'
console.log(app);
// CacheBase {
// cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
// defaults: { foo: 'one', bar: 'two', baz: 'three' } }
```
### [.union](index.js#L199)
Set an array of unique values on cache `key`.
**Params**
* `key` **{String|Array}**: The name of the property to union. Dot-notation may be used to set nested properties.
* `value` **{any}**
* `returns` **{Object}**: Returns the instance for chaining.
**Example**
```js
app.union('a.b.c', 'foo');
app.union('a.b.c', 'bar');
app.union('a.b.c', ['bar', 'baz']);
console.log(app.get('a'));
//=> { b: { c: ['foo', 'bar', 'baz'] } }
```
### [.has](index.js#L223)
Return true if the value of property `key` is not `undefined`.
**Params**
* `key` **{String|Array}**: The name of the property to check. Dot-notation may be used to set nested properties.
* `returns` **{Boolean}**
**Example**
```js
app.set('foo', true);
app.set('baz', null);
app.set('bar', undefined);
app.has('foo'); //=> true
app.has('bar'); //=> true
app.has('baz'); //=> false
```
### [.hasOwn](index.js#L253)
Returns true if the specified property is an own (not inherited) property. Similar to [.has()](#has), but returns true if the key exists, even if the value is `undefined`.
**Params**
* `key` **{String}**
* `returns` **{Boolean}**: Returns true if object `key` exists. Dot-notation may be used to set nested properties.
**Example**
```js
app.set('a.b.c', 'd');
app.set('x', false);
app.set('y', null);
app.set('z', undefined);
app.hasOwn('a'); //=> true
app.hasOwn('b'); //=> true
app.hasOwn('c'); //=> true
app.hasOwn('a.b.c'); //=> true
app.hasOwn('x'); //=> true
app.hasOwn('y'); //=> true
app.hasOwn('z'); //=> true
app.hasOwn('lslsls'); //=> false
```
### [.del](index.js#L278)
Delete one or more properties from the instance.
**Params**
* `key` **{String|Array}**: The name of the property to delete. Dot-notation may be used to set nested properties.
* `returns` **{Object}**: Returns the instance for chaining.
**Events**
* `emits`: `del` with the `key` as the only argument.
**Example**
```js
// setup a listener to update a property with a default
// value when it's deleted by the user
app.on('del', key => app.set(key, app.default(key)));
app.del(); // delete all properties on the cache
// or
app.del('foo');
// or an array of keys
app.del(['foo', 'bar']);
```
### [.clear](index.js#L301)
Reset the entire cache to an empty object. Note that this does not also clear the `defaults` object, since you can manually do `cache.defaults = {}` if you want to reset that object as well.
**Example**
```js
// clear "defaults" whenever the cache is cleared
app.on('clear', key => (app.defaults = {}));
app.clear();
```
### [.visit](index.js#L318)
Visit (or map visit) the specified method (`key`) over the properties in the
given object or array.
**Params**
* `key` **{String|Array}**: The name of the method to visit.
* `val` **{Object|Array}**: The object or array to iterate over.
* `returns` **{Object}**: Returns the instance for chaining.
### [.keys](index.js#L338)
Gets an array of names of all enumerable properties on the cache.
**Example**
```js
const app = new CacheBase();
app.set('user', true);
app.set('admin', false);
console.log(app.keys);
//=> ['user', 'admin']
```
### [.size](index.js#L357)
Gets the length of [keys](#keys).
**Example**
```js
const app = new CacheBase();
app.set('user', true);
app.set('admin', false);
console.log(app.size);
//=> 2
```
## Usage examples
**Create an instance of cache-base**
```js
const app = new CacheBase();
app.set('a', 'b');
app.set('c.d', 'e');
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app);
//=> CacheBase { a: 'b' }
```
**Initialize with an object**
```js
const app = new CacheBase({ a: 'b', c: { d: 'e' } });
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app.get('c.d'));
//=> 'e'
console.log(app);
//=> CacheBase { cache: { a: 'b' } }
```
**Inherit**
```js
class MyApp extends CacheBase {}
const app = new MyApp();
app.set('a', 'b');
app.set('c', 'd');
console.log(app.get('a'));
//=> 'b'
console.log(app);
//=> MyApp { cache: { a: 'b', c: 'd' } }
```
**Custom namespace**
Pass a string as the first value to the contructor to define a custom property name to use for the cache. By default values are stored on the `cache` property.
```js
const CacheBase = require('cache-base');
const app = new CacheBase('data', { a: 'b' });
app.set('c.d', 'e');
// get values
console.log(app.get('a'));
//=> 'b'
console.log(app.get('c'));
//=> { d: 'e' }
console.log(app.data);
//=> { a: 'b', c: { d: 'e' } }
console.log(app);
//=> CacheBase { data: { a: 'b', c: { d: 'e' } } }
```
## About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
```sh
$ npm install && npm test
```
Building docs
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
To generate the readme, run the following command:
```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```
### Related projects
You might also be interested in these projects:
* [base-methods](https://www.npmjs.com/package/base-methods): base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/jonschlinkert/base-methods) | [homepage](https://github.com/jonschlinkert/base-methods "base-methods is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.")
* [get-value](https://www.npmjs.com/package/get-value): Use property paths like 'a.b.c' to get a nested value from an object. Even works… [more](https://github.com/jonschlinkert/get-value) | [homepage](https://github.com/jonschlinkert/get-value "Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!).")
* [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://github.com/jonschlinkert/has-value) | [homepage](https://github.com/jonschlinkert/has-value "Returns true if a value exists, false if empty. Works with deeply nested values using object paths.")
* [option-cache](https://www.npmjs.com/package/option-cache): Simple API for managing options in JavaScript applications. | [homepage](https://github.com/jonschlinkert/option-cache "Simple API for managing options in JavaScript applications.")
* [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 "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.")
* [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value "Delete nested properties from an object using dot notation.")
### Contributors
| **Commits** | **Contributor** |
| --- | --- |
| 67 | [jonschlinkert](https://github.com/jonschlinkert) |
| 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
### Author
**Jon Schlinkert**
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)
### License
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).
***
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 23, 2018._ node-cache-base-4.0.0/examples/ 0000775 0000000 0000000 00000000000 13272150524 0016230 5 ustar 00root root 0000000 0000000 node-cache-base-4.0.0/examples/clear.js 0000664 0000000 0000000 00000000757 13272150524 0017665 0 ustar 00root root 0000000 0000000 const CacheBase = require('..');
const app = new CacheBase();
app.default('foo', 42);
app.default('fez', 42);
app.set('foo', 1);
app.set('bar', 2);
app.set('baz', 3);
app.set('qux', 4);
app.set('fez', 5);
console.log(app.cache);
console.log(app.defaults);
console.log('---');
app.clear();
console.log(app.cache);
console.log(app.defaults);
console.log('---');
app.on('clear', key => (app.defaults = {}));
app.clear();
console.log(app.cache);
console.log(app.defaults);
console.log('---');
node-cache-base-4.0.0/examples/custom-prop.js 0000664 0000000 0000000 00000000551 13272150524 0021057 0 ustar 00root root 0000000 0000000 const Cache = require('..');
const app = new Cache('data');
app.set('a', 'b');
app.set({ c: 'd' });
app.set('e.f', 'g')
console.log(app.get('e.f'));
//=> 'g'
console.log(app.get());
//=> { a: 'b', c: 'd', e: { f: 'g' } }
console.log(app.data);
//=> { a: 'b', c: 'd', e: { f: 'g' } }
console.log(app);
//=> Cache { data: { a: 'b', c: 'd', e: { f: 'g' } } }
node-cache-base-4.0.0/examples/defaults.js 0000664 0000000 0000000 00000000707 13272150524 0020401 0 ustar 00root root 0000000 0000000 const CacheBase = require('..');
const app = new CacheBase();
app.set('foo', 'xxx');
app.default('foo', 'one');
app.default('bar', 'two');
app.default('baz', 'three');
app.set('baz', 'zzz');
console.log(app.get('foo'));
//=> 'xxx'
console.log(app.get('bar'));
//=> 'two'
console.log(app.get('baz'));
//=> 'zzz'
console.log(app);
// Cache {
// cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
// defaults: { foo: 'one', bar: 'two', baz: 'three' } }
node-cache-base-4.0.0/examples/del.js 0000664 0000000 0000000 00000000541 13272150524 0017332 0 ustar 00root root 0000000 0000000 const CacheBase = require('..');
const app = new CacheBase();
// app.on('del', key => app.set(key, app.default(key)));
app.set('foo', 'xxx');
app.default('foo', 'one');
console.log(app.get('foo')); //=> 'xxx'
console.log(app.cache.foo); //=> 'xxx'
app.del('foo');
console.log(app.get('foo')); //=> 'xxx'
console.log(app.cache.foo); //=> undefined
node-cache-base-4.0.0/examples/merge.js 0000664 0000000 0000000 00000001224 13272150524 0017664 0 ustar 00root root 0000000 0000000 const CacheBase = require('..');
const app = new CacheBase();
app.set('foo', 'xxx');
app.default('foo', 'one');
app.default('bar', 'two');
app.default('baz', 'three');
app.default('qux', 'faz');
app.set('baz', 'zzz');
console.log(app.get('foo'));
//=> 'xxx'
console.log(app.get('bar'));
//=> 'two'
console.log(app.get('baz'));
//=> 'zzz'
console.log(app.merge());
//=> { foo: 'xxx', bar: 'two', baz: 'zzz', qux: 'faz' }
console.log(app.merge({qux: 'aaa'}));
//=> { foo: 'xxx', bar: 'two', baz: 'zzz', qux: 'aaa' }
console.log(app);
// Cache {
// cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
// defaults: { foo: 'one', bar: 'two', baz: 'three' } }
node-cache-base-4.0.0/examples/set.js 0000664 0000000 0000000 00000000464 13272150524 0017365 0 ustar 00root root 0000000 0000000 const Cache = require('..');
const app = new Cache();
app.set('a', 'b');
app.set({ c: 'd' });
app.set('e.f', 'g')
console.log(app.get('e.f')); //=> 'g'
console.log(app.get()); //=> { a: 'b', c: 'd', e: { f: 'g' } }
console.log(app.data); //=> { a: 'b', c: 'd', e: { f: 'g' } }
console.log(app);
node-cache-base-4.0.0/index.js 0000664 0000000 0000000 00000023245 13272150524 0016065 0 ustar 00root root 0000000 0000000 'use strict';
const typeOf = require('kind-of');
const Emitter = require('@sellside/emitter');
const visit = require('collection-visit');
const hasOwn = require('has-own-deep');
const union = require('union-value');
const del = require('unset-value');
const get = require('get-value');
const set = require('set-value');
/**
* Create an instance of `CacheBase`.
*
* ```js
* const app = new CacheBase();
* ```
* @param {String|Object} `prop` (optional) Property name to use for the cache, or the object to initialize with.
* @param {Object} `cache` (optional) An object to initialize with.
* @constructor
* @api public
*/
class CacheBase extends Emitter {
constructor(prop, cache) {
super();
if (typeof prop !== 'string') {
cache = prop || cache;
prop = 'cache';
}
Reflect.defineProperty(this, 'prop', { value: prop });
this[this.prop] = {};
if (cache) {
this.set(cache);
}
}
/**
* Assign `value` to `key`. Also emits `set` with the key and value.
*
* ```js
* app.on('set', function(key, val) {
* // do something when `set` is emitted
* });
*
* app.set('admin', true);
*
* // also takes an object or an array of objects
* app.set({ name: 'Brian' });
* app.set([{ foo: 'bar' }, { baz: 'quux' }]);
* console.log(app);
* //=> { name: 'Brian', foo: 'bar', baz: 'quux' }
* ```
* @name .set
* @emits `set` with `key` and `value` as arguments.
* @param {String|Array} `key` The name of the property to set. Dot-notation may be used to set nested properties.
* @param {any} `value`
* @return {Object} Returns the instance for chaining.
* @api public
*/
set(key, ...rest) {
if (isObject(key) || (rest.length === 0 && Array.isArray(key))) {
return this.visit('set', key, ...rest);
}
if (Array.isArray(key)) key = key.join('.');
set(this[this.prop], key, ...rest);
this.emit('set', key, ...rest);
return this;
}
/**
* Return the value of `key`.
*
* ```js
* app.set('a.b.c', 'd');
* app.get('a.b');
* //=> { c: 'd' }
* ```
* @name .get
* @emits `get` with `key` and `value` as arguments.
* @param {String|Array} `key` The name of the property to get. Dot-notation may be used to set nested properties.
* @return {any} Returns the value of `key`
* @api public
*/
get(key) {
if (Array.isArray(key)) key = key.join('.');
let val = get(this[this.prop], key);
if (typeof val === 'undefined' && this.defaults) {
val = get(this.defaults, key);
}
this.emit('get', key, val);
return val;
}
/**
* Create a property on the cache with the given `value` only if it doesn't
* already exist.
*
* ```js
* console.log(app.cache); //=> {}
* app.set('one', { foo: 'bar' });
* app.prime('one', { a: 'b' });
* app.prime('two', { c: 'd' });
* console.log(app.cache.one); //=> { foo: 'bar' }
* console.log(app.cache.two); //=> { c: 'd' }
* ```
* @name .prime
* @param {String} `key` Property name or object path notation.
* @param {any} `val`
* @return {Object} Returns the instance for chaining.
* @api public
*/
prime(key, ...rest) {
if (isObject(key) || (rest.length === 0 && Array.isArray(key))) {
return this.visit('prime', key, ...rest);
}
if (Array.isArray(key)) key = key.join('.');
if (!this.has(key)) {
this.set(key, ...rest);
}
return this;
}
/**
* Set a default value to be used when `.get()` is called and the value is not defined
* on the cache. Returns a value from the defaults when only a key is passed.
*
* ```js
* app.set('foo', 'xxx');
* app.default('foo', 'one');
* app.default('bar', 'two');
* app.default('baz', 'three');
* app.set('baz', 'zzz');
*
* console.log(app.get('foo'));
* //=> 'xxx'
*
* console.log(app.get('bar'));
* //=> 'two'
*
* console.log(app.get('baz'));
* //=> 'zzz'
*
* console.log(app);
* // CacheBase {
* // cache: { foo: 'xxx', bar: 'two', baz: 'zzz' },
* // defaults: { foo: 'one', bar: 'two', baz: 'three' } }
* ```
* @name .default
* @param {String|Array} `key` The name of the property to set. Dot-notation may be used to set nested properties.
* @param {any} `value` (optional) The value to set on the defaults object.
* @return {Object} Returns the instance for chaining.
* @api public
*/
default(key, ...rest) {
this.defaults = this.defaults || {};
if (isObject(key) || (rest.length === 0 && Array.isArray(key))) {
return this.visit('default', key, ...rest);
}
if (Array.isArray(key)) key = key.join('.');
if (!isString(key)) {
throw new TypeError('expected "key" to be a string, object or array');
}
if (rest.length === 0) {
return get(this.defaults, key);
}
set(this.defaults, key, ...rest);
this.emit('default', key, rest);
return this;
}
/**
* Set an array of unique values on cache `key`.
*
* ```js
* app.union('a.b.c', 'foo');
* app.union('a.b.c', 'bar');
* app.union('a.b.c', ['bar', 'baz']);
* console.log(app.get('a'));
* //=> { b: { c: ['foo', 'bar', 'baz'] } }
* ```
* @name .union
* @param {String|Array} `key` The name of the property to union. Dot-notation may be used to set nested properties.
* @param {any} `value`
* @return {Object} Returns the instance for chaining.
* @api public
*/
union(key, ...rest) {
if (Array.isArray(key)) key = key.join('.');
union(this[this.prop], key, ...rest);
this.emit('union', ...rest);
return this;
}
/**
* Return true if the value of property `key` is not `undefined`.
*
* ```js
* app.set('foo', true);
* app.set('baz', null);
* app.set('bar', undefined);
*
* app.has('foo'); //=> true
* app.has('bar'); //=> true
* app.has('baz'); //=> false
* ```
* @name .has
* @param {String|Array} `key` The name of the property to check. Dot-notation may be used to set nested properties.
* @return {Boolean}
* @api public
*/
has(key) {
if (Array.isArray(key)) key = key.join('.');
return typeof get(this[this.prop], key) !== 'undefined';
}
/**
* Returns true if the specified property is an own (not inherited) property.
* Similar to [.has()](#has), but returns true if the key exists, even if the
* value is `undefined`.
*
* ```js
* app.set('a.b.c', 'd');
* app.set('x', false);
* app.set('y', null);
* app.set('z', undefined);
*
* app.hasOwn('a'); //=> true
* app.hasOwn('b'); //=> true
* app.hasOwn('c'); //=> true
* app.hasOwn('a.b.c'); //=> true
* app.hasOwn('x'); //=> true
* app.hasOwn('y'); //=> true
* app.hasOwn('z'); //=> true
* app.hasOwn('lslsls'); //=> false
* ```
* @name .hasOwn
* @param {String} `key`
* @return {Boolean} Returns true if object `key` exists. Dot-notation may be used to set nested properties.
* @api public
*/
hasOwn(key) {
if (Array.isArray(key)) key = key.join('.');
return hasOwn(this[this.prop], key);
}
/**
* Delete one or more properties from the instance.
*
* ```js
* // setup a listener to update a property with a default
* // value when it's deleted by the user
* app.on('del', key => app.set(key, app.default(key)));
*
* app.del(); // delete all properties on the cache
* // or
* app.del('foo');
* // or an array of keys
* app.del(['foo', 'bar']);
* ```
* @name .del
* @emits `del` with the `key` as the only argument.
* @param {string} `key` The name of the property to delete. Dot-notation may be used to delete nested properties. This method does not accept key as an array.
* @return {Object} Returns the instance for chaining.
* @api public
*/
del(key) {
if (!key) return this.clear();
del(this[this.prop], key);
this.emit('del', key);
return this;
}
/**
* Reset the entire cache to an empty object. Note that this does not also clear the `defaults`
* object, since you can manually do `cache.defaults = {}` if you want to reset that object as well.
*
* ```js
* // clear "defaults" whenever the cache is cleared
* app.on('clear', key => (app.defaults = {}));
* app.clear();
* ```
* @name .clear
* @api public
*/
clear() {
this[this.prop] = {};
this.emit('clear');
return this;
}
/**
* Visit (or map visit) the specified method (`key`) over the properties in the
* given object or array.
*
* @name .visit
* @param {String|Array} `key` The name of the method to visit.
* @param {Object|Array} `val` The object or array to iterate over.
* @return {Object} Returns the instance for chaining.
* @api public
*/
visit(key, ...rest) {
visit(this, key, ...rest);
return this;
}
/**
* Gets an array of names of all enumerable properties on the cache.
*
* ```js
* const app = new CacheBase();
* app.set('user', true);
* app.set('admin', false);
*
* console.log(app.keys);
* //=> ['user', 'admin']
* ```
* @name .keys
* @api public
*/
get keys() {
return Object.keys(this[this.prop]);
}
/**
* Gets the length of [keys](#keys).
*
* ```js
* const app = new CacheBase();
* app.set('user', true);
* app.set('admin', false);
*
* console.log(app.size);
* //=> 2
* ```
* @name .size
* @api public
*/
get size() {
return this.keys.length;
}
}
/**
* Returns true if `value` is a non-empty string.
*/
function isString(value) {
return typeof value === 'string' && value !== '';
}
/**
* Returns true if `value` is an object
*/
function isObject(value) {
return typeOf(value) === 'object';
}
/**
* Expose `CacheBase`
*/
module.exports = CacheBase;
node-cache-base-4.0.0/package.json 0000664 0000000 0000000 00000003247 13272150524 0016706 0 ustar 00root root 0000000 0000000 {
"name": "cache-base",
"description": "Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.",
"version": "4.0.0",
"homepage": "https://github.com/jonschlinkert/cache-base",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"contributors": [
"Jon Schlinkert (http://twitter.com/jonschlinkert)",
"(https://github.com/wtgtybhertgeghgtwtg)"
],
"repository": "jonschlinkert/cache-base",
"bugs": {
"url": "https://github.com/jonschlinkert/cache-base/issues"
},
"license": "MIT",
"files": [
"index.js"
],
"main": "index.js",
"engines": {
"node": ">=6"
},
"scripts": {
"test": "nyc --reporter=text --reporter=html mocha"
},
"dependencies": {
"@sellside/emitter": "^1.2.1",
"collection-visit": "^1.0.0",
"get-value": "^3.0.1",
"has-own-deep": "^1.1.0",
"kind-of": "^6.0.2",
"set-value": "^3.0.0",
"union-value": "^1.0.0",
"unset-value": "^1.0.0"
},
"devDependencies": {
"gulp-format-md": "^1.0.0",
"mocha": "^3.5.3",
"nyc": "^11.6.0"
},
"keywords": [
"base",
"cache",
"config",
"data",
"get",
"has",
"hash",
"hasown",
"object",
"set",
"store"
],
"verb": {
"run": true,
"toc": true,
"layout": "default",
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"related": {
"highligh": "base",
"list": [
"base-methods",
"get-value",
"has-value",
"option-cache",
"set-value",
"unset-value"
]
},
"reflinks": [
"verb"
],
"lint": {
"reflinks": true
}
}
}
node-cache-base-4.0.0/test/ 0000775 0000000 0000000 00000000000 13272150524 0015371 5 ustar 00root root 0000000 0000000 node-cache-base-4.0.0/test/custom-property.js 0000664 0000000 0000000 00000005547 13272150524 0021136 0 ustar 00root root 0000000 0000000 /*!
* cache-base
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
require('mocha');
const assert = require('assert');
const Cache = require('../');
let app, cache;
describe('custom property', function() {
beforeEach(function() {
app = new Cache('data');
});
describe('constructor:', function() {
it('should create an instance of Cache', function() {
assert(app instanceof Cache);
});
it('should set ', function() {
app = new Cache('data', { one: 1, two: 2 });
assert.equal(app.data.one, 1);
assert.equal(app.data.two, 2);
});
});
describe('get/set:', function() {
describe('set() - add:', function() {
it('should set a new property with the given value', function() {
app.set('one', 1);
assert.equal(app.get('one'), 1);
assert.equal(app.data.one, 1);
});
});
describe('set() - update:', function() {
it('should update an existing property with the given value', function() {
app.set('one', 2);
assert.equal(app.get('one'), 2);
assert.equal(app.data.one, 2);
});
it('should get the given property', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
assert.equal(app.data.a, 'b');
});
});
});
describe('.set()', function() {
it('should set a value', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
assert.equal(app.data.a, 'b');
});
it('should set properties on the `data` object', function() {
app.set('a', 'b');
assert.equal(app.data.a, 'b');
});
it('should allow an object to be set directly', function() {
app.set({x: 'y'});
assert.equal(app.data.x, 'y');
assert.equal(app.get('x'), 'y');
});
it('should set nested properties on the `data` object', function() {
app.set('c', {d: 'e'});
assert.equal(app.get('c').d, 'e');
});
it('should return the instance', function() {
assert.equal(app.set('a', 'b'), app);
});
it('should be chainable', function() {
app
.set('aa', 'bb')
.set('bb', 'cc')
.set('cc', 'dd');
assert.equal(app.get('aa'), 'bb');
assert.equal(app.get('bb'), 'cc');
assert.equal(app.get('cc'), 'dd');
assert.equal(app.data.aa, 'bb');
assert.equal(app.data.bb, 'cc');
assert.equal(app.data.cc, 'dd');
});
it('should return undefined when not set', function() {
assert.equal(app.set('sfsfsdfs', undefined), app);
});
});
describe('.get()', function() {
it('should otherwise return the value', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
assert.equal(app.data.a, 'b');
assert.equal(app.get('zllzzl'), undefined);
});
});
});
node-cache-base-4.0.0/test/events.js 0000664 0000000 0000000 00000004217 13272150524 0017237 0 ustar 00root root 0000000 0000000 /*!
* cache-base
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
require('mocha');
const assert = require('assert');
const Cache = require('../');
let app, cache;
describe('events', function() {
beforeEach(function() {
app = new Cache();
});
describe('set', function() {
it('should emit a "set" event', function(cb) {
app.on('set', () => cb());
app.set('a', 'b');
});
it('should emit the key with "set" events', function(cb) {
app.on('set', function(key, val) {
assert.equal(key, 'a');
cb();
});
app.set('a', 'b');
});
it('should emit the value with "set" events', function(cb) {
app.on('set', function(key, val) {
assert.equal(val, 'b');
cb();
});
app.set('a', 'b');
});
});
describe('get', function() {
it('should emit a get event', function(cb) {
app.on('get', () => cb());
app.get('a');
});
it('should emit the key with "get" events', function(cb) {
app.on('get', function(key, val) {
assert.equal(key, 'a');
cb();
});
app.set('a', 'b');
app.get('a');
});
it('should emit the value with "get" events', function(cb) {
app.on('get', function(key, val) {
assert.equal(val, 'b');
cb();
});
app.set('a', 'b');
app.get('a');
});
});
describe('del', function() {
it('should emit a del event', function(cb) {
app.on('del', () => cb());
app.del('a');
});
it('should emit the key with "del" events', function(cb) {
app.on('del', function(key) {
assert.equal(key, 'a');
cb();
});
app.set('a', 'b');
app.del('a');
});
it('should emit each deleted key when multiple properties are deleted', function(cb) {
var keys = [];
app.on('del', key => keys.push(key));
app.set('a', 'b');
app.set('c', 'd');
app.del('a');
app.del('c');
assert.deepEqual(keys, ['a', 'c']);
assert(!app.a);
assert(!app.c);
cb();
});
});
});
node-cache-base-4.0.0/test/fixtures/ 0000775 0000000 0000000 00000000000 13272150524 0017242 5 ustar 00root root 0000000 0000000 node-cache-base-4.0.0/test/fixtures/a.yml 0000664 0000000 0000000 00000000004 13272150524 0020177 0 ustar 00root root 0000000 0000000 a: b node-cache-base-4.0.0/test/fixtures/data/ 0000775 0000000 0000000 00000000000 13272150524 0020153 5 ustar 00root root 0000000 0000000 node-cache-base-4.0.0/test/fixtures/data/alert.json 0000664 0000000 0000000 00000000170 13272150524 0022153 0 ustar 00root root 0000000 0000000 {
"success": {
"test": true,
"strong": "Heads up! This is a warning!",
"text": "You forgot a field!"
}
} node-cache-base-4.0.0/test/fixtures/data/data.json 0000664 0000000 0000000 00000000056 13272150524 0021760 0 ustar 00root root 0000000 0000000 {
"root": "Whoa, I should be at the root!"
} node-cache-base-4.0.0/test/fixtures/data/test.json 0000664 0000000 0000000 00000000045 13272150524 0022024 0 ustar 00root root 0000000 0000000 {
"alpha": "one",
"beta": "two"
} node-cache-base-4.0.0/test/fixtures/namespace/ 0000775 0000000 0000000 00000000000 13272150524 0021176 5 ustar 00root root 0000000 0000000 node-cache-base-4.0.0/test/fixtures/namespace/a.yml 0000664 0000000 0000000 00000000020 13272150524 0022131 0 ustar 00root root 0000000 0000000 one: ${site.two} node-cache-base-4.0.0/test/fixtures/namespace/b.yml 0000664 0000000 0000000 00000000022 13272150524 0022134 0 ustar 00root root 0000000 0000000 two: ${site.three} node-cache-base-4.0.0/test/fixtures/namespace/c.json 0000664 0000000 0000000 00000000044 13272150524 0022311 0 ustar 00root root 0000000 0000000 {
"three": "data was processed!"
} node-cache-base-4.0.0/test/test.js 0000664 0000000 0000000 00000022270 13272150524 0016711 0 ustar 00root root 0000000 0000000 /*!
* cache-base
*
* Copyright (c) 2014-2018, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
require('mocha');
const assert = require('assert');
const Cache = require('../');
let app, cache;
describe('cache-base', function() {
beforeEach(function() {
app = new Cache();
});
describe('constructor:', function() {
it('should create an instance of Cache', function() {
assert(app instanceof Cache);
});
it('should set values passed on the ctor', function() {
app = new Cache({ one: 1, two: 2 });
assert.equal(app.cache.one, 1);
assert.equal(app.cache.two, 2);
});
});
describe('prime', function() {
it('should prime a property on the cache with the given value', function() {
app.prime('life', 42);
assert.equal(app.cache.life, 42);
});
it('should not prime a property if it already exists', function() {
app.set('life', 51);
app.prime('life', 42);
assert.equal(app.cache.life, 51);
});
it('should prime an object', function() {
app.prime({ life: 42 });
assert.equal(app.cache.life, 42);
});
});
describe('default', function() {
it('should set a default value on cache.defaults', function() {
app.default('life', 42);
assert.equal(app.defaults.life, 42);
});
it('should not set a default value on the cache object', function() {
app.default('life', 42);
assert.equal(app.cache.life, undefined);
});
it('should be overridden when values are set', function() {
app.default('life', 42);
app.set('life', 51);
assert.equal(app.cache.life, 51);
});
it('should not set a default if it already exists', function() {
app.set('life', 51);
app.default('life', 42);
assert.equal(app.cache.life, 51);
});
it('should take an object', function() {
app.default({ life: 41 });
assert.equal(app.defaults.life, 41);
});
it('should take an array of objects', function() {
app.default([{ meaning: 41, life: 42 }]);
assert.equal(app.defaults.meaning, 41);
assert.equal(app.defaults.life, 42);
});
it('should return a value from cache.defaults when only the key is passed', function() {
app.default({ foo: 1, bar: 2 });
assert.equal(app.default('foo'), 1);
});
it('should return the default value with `.get()`', function() {
app.default({ foo: 1, bar: 2 });
app.set('foo', 42);
assert.equal(app.get('foo'), 42);
app.del('foo');
assert.equal(app.get('foo'), 1);
assert.equal(app.get('bar'), 2);
});
});
describe('set', function() {
it('should set a value', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
});
it('should set properties on `app.cache` when defined as key-value pairs', function() {
app.set('a', 'b');
assert.equal(app.cache.a, 'b');
});
it('should set properties on `app.cache` when defined as as an object', function() {
app.set({x: 'y'});
assert.equal(app.cache.x, 'y');
assert.equal(app.get('x'), 'y');
});
it('should set nested properties on the `app.cache`', function() {
app.set('c', {d: 'e'});
assert.equal(app.get('c.d'), 'e');
});
it('should be chainable', function() {
assert.equal(app.set('a', 'b'), app);
app
.set('aa', 'bb')
.set('bb', 'cc')
.set('cc', 'dd');
assert.equal(app.get('aa'), 'bb');
assert.equal(app.get('bb'), 'cc');
assert.equal(app.get('cc'), 'dd');
});
it('should return undefined when not set', function() {
assert.equal(app.set('a', undefined), app);
});
});
describe('get', function() {
it('should return undefined when no set', function() {
assert(app.get('a') === undefined);
});
it('should get a value', function() {
app.set('a', 'b');
assert.equal(app.get('a'), 'b');
});
it('should get a nested property value', function() {
app.set('a.b.c', 'z');
assert.equal(app.cache.a.b.c, 'z');
assert.deepEqual(app.get('a.b'), {c: 'z'});
});
it('should support passing key as an array', function() {
app.set('a.b.c', 'z');
assert.equal(app.cache.a.b.c, 'z');
assert.deepEqual(app.get(['a', 'b']), {c: 'z'});
});
});
describe('union', function() {
it('should union a string value', function() {
app.union('a', 'b');
assert.deepEqual(app.get('a'), ['b']);
});
it('should union multiple string values', function() {
app.union('a', 'b');
app.union('a', 'c');
app.union('a', 'd');
assert.deepEqual(app.get('a'), ['b', 'c', 'd']);
});
it('should union multiple arrays', function() {
app.union('a', ['b']);
app.union('a', ['c']);
app.union('a', ['d']);
assert.deepEqual(app.get('a'), ['b', 'c', 'd']);
});
it('should union strings and arrays', function() {
app.union('a', 'a');
app.union('a', ['b']);
app.union('a', ['c', 'd']);
assert.deepEqual(app.get('a'), ['a', 'b', 'c', 'd']);
});
it('should union nested string values', function() {
app.union('a.b', 'b');
app.union('a.b', 'c');
app.union('a.b', 'd');
assert.deepEqual(app.get('a'), {b: ['b', 'c', 'd']});
});
it('should union and uniquify arrays', function() {
app.union('a.b', ['b', 'foo']);
app.union('a.b', ['c', 'foo']);
app.union('a.b', ['d', 'foo']);
assert.deepEqual(app.get('a'), {b: ['b', 'foo', 'c', 'd']});
});
});
describe('has', function() {
it('should return true if cache has a value for the given key', function() {
app.set('foo', 'bar');
app.set('baz', null);
app.set('qux', undefined);
assert(app.has('foo'));
assert(!app.has('bar'));
assert(app.has('baz'));
assert(!app.has('qux'));
});
it('should work with escaped keys', function() {
app.set('foo\\.baz', 'bar');
assert(!app.has('foo'));
assert(!app.has('bar'));
assert(app.has('foo.baz'));
});
it('should return true if a nested key value on the cache', function() {
app.set('a.b.c.d', { x: 'zzz' });
app.set('a.b.c.e', { f: null });
app.set('a.b.g.j', { k: undefined });
assert(app.has('a'));
assert(app.has('a.b'));
assert(app.has('a.b.c'));
assert(app.has('a.b.c.d'));
assert(app.has('a.b.c.d.x'));
assert(app.has('a.b.c.e.f'));
assert(!app.has('a.b.g.j.k'));
assert(!app.has('a.b.bar'));
assert(!app.has('a.b.c.d.z'));
assert(!app.has('a.b.c.e.bar'));
assert(!app.has('a.b.g.j.foo'));
});
});
describe('hasOwn', function() {
it('should return true if a cache has own key', function() {
app.set('foo', 'bar');
app.set('baz', null);
app.set('qux', undefined);
assert(app.hasOwn('foo'));
assert(!app.hasOwn('bar'));
assert(app.hasOwn('baz'));
assert(app.hasOwn('qux'));
});
it('should work with escaped keys', function() {
app.set('foo\\.baz', 'bar');
app.set('baz', null);
app.set('qux', undefined);
assert(!app.hasOwn('foo'));
assert(!app.hasOwn('bar'));
assert(app.hasOwn('foo.baz'));
assert(app.hasOwn('baz'));
assert(app.hasOwn('qux'));
});
it('should return true if a nested key exists `.hasOwn()` on the cache', function() {
app.set('a.b.c.d', { x: 'zzz' });
app.set('a.b.c.e', { f: null });
app.set('a.b.g.j', { k: undefined });
assert(app.hasOwn('a'));
assert(app.hasOwn('a.b'));
assert(app.hasOwn('a.b.c'));
assert(app.hasOwn('a.b.c.d'));
assert(app.hasOwn('a.b.c.d.x'));
assert(app.hasOwn('a.b.c.e.f'));
assert(app.hasOwn('a.b.g.j.k'));
assert(app.hasOwn('a.b.g.j.k'));
assert(app.hasOwn('a.b.c.e.f'));
assert(!app.hasOwn('a.b.bar'));
assert(!app.hasOwn('a.b.c.d.z'));
assert(!app.hasOwn('a.b.c.e.bar'));
assert(!app.hasOwn('a.b.g.j.foo'));
});
});
describe('del', function() {
it('should delete a property from the cache', function() {
app.set('foo', 42);
app.set('bar', 43);
assert.equal(app.get('foo'), 42);
assert.equal(app.get('bar'), 43);
app.del('foo');
assert.equal(app.get('foo'), undefined);
assert.equal(app.get('bar'), 43);
});
it('should delete all property from the cache when no key is passed', function() {
app.set('foo', 42);
app.set('bar', 43);
assert.equal(app.get('foo'), 42);
assert.equal(app.get('bar'), 43);
app.del();
assert.equal(app.get('foo'), undefined);
assert.equal(app.get('bar'), undefined);
});
});
describe('keys', function() {
it('should return all enumerable property names from the cache', function() {
app.set('foo', 42);
app.set('bar', null);
app.set('baz', undefined);
assert.deepEqual(app.keys, ['foo', 'bar', 'baz']);
});
});
describe('size', function() {
it('should return the length of cache.keys', function() {
app.set('foo', 'bar');
app.set('baz', null);
app.set('qux', undefined);
assert.equal(app.size, 3);
});
});
});