Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
feat(storage): rename set() into put()
Browse files Browse the repository at this point in the history
The new api (put, get) is capable with AJS' $cacheFactory.

BREAKING CHANGE: This marks storage.set() as deprecated. In the
next major release v3, the old method `set()` will be dropped in favor
of `put()`.

Relates #772
  • Loading branch information
knalli committed Oct 21, 2014
1 parent 06818ab commit ef6a613
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/service/storage-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,28 @@ angular.module('pascalprecht.translate')
* @description
* Sets an item in cookieStorage by given name.
*
* @deprecated use #put
*
* @param {string} name Item name
* @param {string} value Item value
*/
set: function (name, value) {
$cookieStore.put(name, value);
},

/**
* @ngdoc function
* @name pascalprecht.translate.$translateCookieStorage#put
* @methodOf pascalprecht.translate.$translateCookieStorage
*
* @description
* Sets an item in cookieStorage by given name.
*
* @param {string} name Item name
* @param {string} value Item value
*/
put: function (name, value) {
$cookieStore.put(name, value);
}
};

Expand Down
17 changes: 17 additions & 0 deletions src/service/storage-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,29 @@ angular.module('pascalprecht.translate')
* @description
* Sets an item in localStorage by given name.
*
* @deprecated use #put
*
* @param {string} name Item name
* @param {string} value Item value
*/
set: function (name, value) {
langKey=value;
$window.localStorage.setItem(name, value);
},
/**
* @ngdoc function
* @name pascalprecht.translate.$translateLocalStorage#put
* @methodOf pascalprecht.translate.$translateLocalStorage
*
* @description
* Sets an item in localStorage by given name.
*
* @param {string} name Item name
* @param {string} value Item value
*/
put: function (name, value) {
langKey=value;
$window.localStorage.setItem(name, value);
}
};
}());
Expand Down
6 changes: 3 additions & 3 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
$rootScope.$emit('$translateChangeSuccess', {language: key});

if ($storageFactory) {
Storage.set($translate.storageKey(), $uses);
Storage.put($translate.storageKey(), $uses);
}
// inform default interpolator
defaultInterpolator.setLocale($uses);
Expand Down Expand Up @@ -977,8 +977,8 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
if ($storageFactory) {
Storage = $injector.get($storageFactory);

if (!Storage.get || !Storage.set) {
throw new Error('Couldn\'t use storage \'' + $storageFactory + '\', missing get() or set() method!');
if (!Storage.get || !Storage.put) {
throw new Error('Couldn\'t use storage \'' + $storageFactory + '\', missing get() or put() method!');
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ angular.module('pascalprecht.translate', ['ng'])
if (angular.isString($translate.preferredLanguage())) {
$translate.use($translate.preferredLanguage());
// $translate.use() will also remember the language.
// So, we don't need to call storage.set() here.
// So, we don't need to call storage.put() here.
} else {
storage.set(key, $translate.use());
storage.put(key, $translate.use());
}

} else {
Expand Down
13 changes: 8 additions & 5 deletions test/unit/service/storage-cookie.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ describe('pascalprecht.translate', function () {
expect(typeof $translateCookieStorage).toBe('object');
});

it('should have a set() and a get() method', function () {
it('should have a put() and a get() method', function () {
expect($translateCookieStorage.put).toBeDefined();
expect($translateCookieStorage.get).toBeDefined();
expect($translateCookieStorage.set).toBeDefined();
expect($translateCookieStorage.set).toBeDefined(); // deprecated
});

describe('$translateCookieStorage#get', function () {
Expand All @@ -30,9 +31,10 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translateCookieStorage#set', function () {
describe('$translateCookieStorage#put', function () {
it('should be a function', function () {
expect(typeof $translateCookieStorage.set).toBe('function');
expect(typeof $translateCookieStorage.set).toBe('function'); // deprecated
expect(typeof $translateCookieStorage.put).toBe('function');
});
});
});
Expand Down Expand Up @@ -82,7 +84,8 @@ describe('pascalprecht.translate', function () {

it('should return registered storage instance if exists', function () {
expect(typeof $translate.storage()).toBe('object');
expect($translate.storage().set).toBeDefined();
expect($translate.storage().set).toBeDefined(); // deprecated
expect($translate.storage().put).toBeDefined();
expect($translate.storage().get).toBeDefined();
});
});
Expand Down
10 changes: 6 additions & 4 deletions test/unit/service/storage-local.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ describe('pascalprecht.translate', function () {
expect(typeof $translateLocalStorage).toBe('object');
});

it('should have a set() and a get() method', function () {
it('should have a put() and a get() method', function () {
expect($translateLocalStorage.put).toBeDefined();
expect($translateLocalStorage.get).toBeDefined();
expect($translateLocalStorage.set).toBeDefined();
expect($translateLocalStorage.set).toBeDefined(); // deprecated
});

describe('$translateLocalStorage#get', function () {
Expand All @@ -30,9 +31,10 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translateLocalStorage#set()', function () {
describe('$translateLocalStorage#put()', function () {
it('should be a function', function () {
expect(typeof $translateLocalStorage.set).toBe('function');
expect(typeof $translateLocalStorage.set).toBe('function'); // deprecated
expect(typeof $translateLocalStorage.put).toBe('function');
});
});

Expand Down

0 comments on commit ef6a613

Please sign in to comment.