Skip to content

Commit

Permalink
Merge pull request #78 from jmdobry/fix-allow-number-keys
Browse files Browse the repository at this point in the history
Accept numbers as keys, but stringify them. #76
  • Loading branch information
jmdobry committed Nov 20, 2013
2 parents e9eeac0 + fad83bb commit 224d99b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
21 changes: 20 additions & 1 deletion dist/angular-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@
this.$get = ['$window', 'BinaryHeap', function ($window, BinaryHeap) {
var caches = {};

/**
* Stringify a number.
* @param {number|*} number The number to be stringified.
* @returns {*} number or a string.
* @private
*/
function _stringifyNumber(number) {
if (number && angular.isNumber(number)) {
return number.toString();
}
return number;
}

/**
* @method _keySet
* @desc Returns an object of the keys of the given collection.
Expand Down Expand Up @@ -631,7 +644,7 @@
storage.removeItem(prefix + '.keys');
if (keys && keys.length) {
for (var i = 0; i < keys.length; i++) {
var data = angular.fromJson(storage.getItem(prefix + '.data.' + keys[i])),
var data = angular.fromJson(storage.getItem(prefix + '.data.' + keys[i])) || {},
maxAge = data.maxAge || config.maxAge,
deleteOnExpire = data.deleteOnExpire || config.deleteOnExpire;
if (maxAge && ((new Date().getTime() - data.created) > maxAge) && deleteOnExpire === 'aggressive') {
Expand Down Expand Up @@ -718,6 +731,9 @@
*/
this.put = function (key, value, options) {
options = options || {};

key = _stringifyNumber(key);

if (!angular.isString(key)) {
throw new Error('AngularCache.put(key, value, options): key: must be a string!');
} else if (options && !angular.isObject(options)) {
Expand Down Expand Up @@ -791,6 +807,7 @@
* @returns {*} The value of the item in the cache with the specified key.
*/
this.get = function (key, options) {

if (angular.isArray(key)) {
var keys = key,
values = [];
Expand All @@ -803,6 +820,8 @@
});

return values;
} else {
key = _stringifyNumber(key);
}

options = options || {};
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-cache.min.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/angular-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,19 @@
this.$get = ['$window', 'BinaryHeap', function ($window, BinaryHeap) {
var caches = {};

/**
* Stringify a number.
* @param {number|*} number The number to be stringified.
* @returns {*} number or a string.
* @private
*/
function _stringifyNumber(number) {
if (number && angular.isNumber(number)) {
return number.toString();
}
return number;
}

/**
* @method _keySet
* @desc Returns an object of the keys of the given collection.
Expand Down Expand Up @@ -718,6 +731,9 @@
*/
this.put = function (key, value, options) {
options = options || {};

key = _stringifyNumber(key);

if (!angular.isString(key)) {
throw new Error('AngularCache.put(key, value, options): key: must be a string!');
} else if (options && !angular.isObject(options)) {
Expand Down Expand Up @@ -791,6 +807,7 @@
* @returns {*} The value of the item in the cache with the specified key.
*/
this.get = function (key, options) {

if (angular.isArray(key)) {
var keys = key,
values = [];
Expand All @@ -803,6 +820,8 @@
});

return values;
} else {
key = _stringifyNumber(key);
}

options = options || {};
Expand Down
8 changes: 4 additions & 4 deletions test/angularCache.get-test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
describe('AngularCache.get(key)', function () {
it('should throw an error if "key" is not a string or array.', function () {
var cache = $angularCacheFactory('cache');
for (var i = 0; i < TYPES_EXCEPT_STRING_OR_ARRAY.length; i++) {
for (var i = 0; i < TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER.length; i++) {
try {
cache.get(TYPES_EXCEPT_STRING_OR_ARRAY[i]);
fail(TYPES_EXCEPT_STRING_OR_ARRAY[i]);
cache.get(TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER[i]);
fail(TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER[i]);
} catch (err) {
expect(err.message).toEqual('AngularCache.get(key, options): key: must be a string!');
continue;
}
fail(TYPES_EXCEPT_STRING_OR_ARRAY[i]);
fail(TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER[i]);
}
});
it('should throw an error if "options" is not an object.', function () {
Expand Down
8 changes: 4 additions & 4 deletions test/angularCache.put-test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
describe('AngularCache.put(key, value, options)', function () {
it('should throw an error if "key" is not a string.', function () {
var cache = $angularCacheFactory('cache');
for (var i = 0; i < TYPES_EXCEPT_STRING.length; i++) {
for (var i = 0; i < TYPES_EXCEPT_STRING_OR_NUMBER.length; i++) {
try {
cache.put(TYPES_EXCEPT_STRING[i], 'value');
fail(TYPES_EXCEPT_STRING[i]);
cache.put(TYPES_EXCEPT_STRING_OR_NUMBER[i], 'value');
fail(TYPES_EXCEPT_STRING_OR_NUMBER[i]);
} catch (err) {
expect(err.message).toEqual('AngularCache.put(key, value, options): key: must be a string!');
continue;
}
fail(TYPES_EXCEPT_STRING[i]);
fail(TYPES_EXCEPT_STRING_OR_NUMBER[i]);
}
});
it('should throw an error if "options" is not an object.', function () {
Expand Down
2 changes: 2 additions & 0 deletions test/karma.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var fail = function (msg) {
},
TYPES_EXCEPT_STRING = [123, 123.123, null, undefined, {}, [], true, false, function () {}],
TYPES_EXCEPT_STRING_OR_ARRAY = [123, 123.123, null, undefined, {}, true, false, function () {}],
TYPES_EXCEPT_STRING_OR_NUMBER = [null, undefined, {}, [], true, false, function () {}],
TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER = [null, undefined, {}, true, false, function () {}],
TYPES_EXCEPT_NUMBER = ['string', null, undefined, {}, [], true, false, function () {}],
TYPES_EXCEPT_OBJECT = ['string', 123, 123.123, null, undefined, true, false, function () {}],
TYPES_EXCEPT_BOOLEAN = ['string', 123, 123.123, null, undefined, {}, [], function () {}],
Expand Down

0 comments on commit 224d99b

Please sign in to comment.