Skip to content

Commit 9fe9065

Browse files
committed
adds .union method. resolves #3
1 parent 267180a commit 9fe9065

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ function namespace(prop) {
8686
return this;
8787
};
8888

89+
/**
90+
* Union `array` to `key`. Also emits `set` with
91+
* the key and value.
92+
*
93+
* ```js
94+
* app.union('a.b', ['foo']);
95+
* app.union('a.b', ['bar']);
96+
* console.log(app.get('a'));
97+
* //=> {b: ['foo', 'bar']}
98+
* ```
99+
* @name .union
100+
* @param {String} `key`
101+
* @param {any} `value`
102+
* @return {Object} Returns the instance for chaining.
103+
* @api public
104+
*/
105+
106+
Cache.prototype.union = function(key, val) {
107+
var ctx = prop ? this[prop] : this;
108+
utils.union(ctx, key, utils.arrayify(val));
109+
return this;
110+
};
111+
89112
/**
90113
* Return the value of `key`. Dot notation may be used
91114
* to get [nested property values][get-value].

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "cache-base",
33
"description": "Basic object cache with `get`, `set`, `del`, and `has` methods for node.js/javascript projects.",
4-
"version": "0.8.2",
4+
"version": "0.8.3",
55
"homepage": "https://github.com/jonschlinkert/cache-base",
66
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
77
"repository": "jonschlinkert/cache-base",
@@ -22,18 +22,19 @@
2222
},
2323
"dependencies": {
2424
"collection-visit": "^0.2.1",
25-
"component-emitter": "^1.2.0",
26-
"get-value": "^2.0.3",
27-
"has-value": "^0.3.0",
25+
"component-emitter": "^1.2.1",
26+
"get-value": "^2.0.5",
27+
"has-value": "^0.3.1",
2828
"isobject": "^2.0.0",
2929
"lazy-cache": "^1.0.3",
3030
"set-value": "^0.3.3",
3131
"to-object-path": "^0.3.0",
32+
"union-value": "^0.2.3",
3233
"unset-value": "^0.1.1"
3334
},
3435
"devDependencies": {
35-
"gulp-format-md": "^0.1.7",
36-
"mocha": "*"
36+
"gulp-format-md": "^0.1.8",
37+
"mocha": "^2.4.5"
3738
},
3839
"keywords": [
3940
"cache",

test/test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,41 @@ describe('cache-base', function() {
6464
});
6565
});
6666

67+
describe('.union()', function() {
68+
it('should union a string value', function() {
69+
app.union('a', 'b');
70+
assert.deepEqual(app.get('a'), ['b']);
71+
});
72+
73+
it('should union multiple string values', function() {
74+
app.union('a', 'b');
75+
app.union('a', 'c');
76+
app.union('a', 'd');
77+
assert.deepEqual(app.get('a'), ['b', 'c', 'd']);
78+
});
79+
80+
it('should union multiple arrays', function() {
81+
app.union('a', ['b']);
82+
app.union('a', ['c']);
83+
app.union('a', ['d']);
84+
assert.deepEqual(app.get('a'), ['b', 'c', 'd']);
85+
});
86+
87+
it('should union nested string values', function() {
88+
app.union('a.b', 'b');
89+
app.union('a.b', 'c');
90+
app.union('a.b', 'd');
91+
assert.deepEqual(app.get('a'), {b: ['b', 'c', 'd']});
92+
});
93+
94+
it('should union and uniquify arrays', function() {
95+
app.union('a.b', ['b', 'foo']);
96+
app.union('a.b', ['c', 'foo']);
97+
app.union('a.b', ['d', 'foo']);
98+
assert.deepEqual(app.get('a'), {b: ['b', 'foo', 'c', 'd']});
99+
});
100+
});
101+
67102
describe('.set()', function() {
68103
it('should set a value', function() {
69104
app.set('a', 'b');

utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,18 @@ require('unset-value', 'del');
1919
require('get-value', 'get');
2020
require('has-value', 'has');
2121
require('set-value', 'set');
22+
require('union-value', 'union');
2223
require('to-object-path', 'toPath');
2324
require = fn;
2425

26+
/**
27+
* Cast val to an array
28+
*/
29+
30+
utils.arrayify = function(val) {
31+
return val ? (Array.isArray(val) ? val : [val]) : [];
32+
};
33+
2534
/**
2635
* Expose `utils` modules
2736
*/

0 commit comments

Comments
 (0)