diff --git a/angular-locker/angular-locker-tests.ts b/angular-locker/angular-locker-tests.ts new file mode 100644 index 0000000000..0a1e107b0a --- /dev/null +++ b/angular-locker/angular-locker-tests.ts @@ -0,0 +1,133 @@ +/// +/// + +angular +.module('angular-locker-tests', ['angular-locker']) +.config(['lockerProvider', function config(lockerProvider) { + let lockerSettings: angular.locker.ILockerSettings = { + driver: 'session', + namespace: 'myApp', + separator: '.', + eventsEnabled: true, + extend: {} + }; + + lockerProvider.defaults(lockerSettings); +}]) +.controller('LockerController', ['$scope', 'locker', function ($scope: angular.IScope, locker: angular.locker.ILockerService) { + locker.put('someKey', 'someVal'); + + // put an item into session storage + locker.driver('session').put('sessionKey', ['some', 'session', 'data']); + + // add an item within a different namespace + locker.namespace('otherNamespace').put('foo', 'bar'); + + locker.put('someString', 'anyDataType'); + locker.put('someObject', { foo: 'I will be serialized', bar: 'pretty cool eh' }); + locker.put('someArray', ['foo', 'bar', 'baz']); + // etc + + //Inserts specified key and return value of function + locker.put('someKey', function() { + var obj = { foo: 'bar', bar: 'baz' }; + // some other logic + return obj; + }); + + locker.put('someKey', ['foo', 'bar']); + + //The current value will be passed into the function so you can perform logic on the current value, before returning it. e.g. + locker.put('someKey', function(current) { + current.push('baz'); + + return current; + }); + + locker.get('someKey'); // = ['foo', 'bar', 'baz'] + + // given locker.get('foo') is not defined + locker.put('foo', function (current) { + // current will equal 'bar' + }, 'bar'); + + //This will add each key/value pair as a separate item in storage + locker.put({ + someKey: 'johndoe', + anotherKey: ['some', 'random', 'array'], + boolKey: true + }); + + locker.add('someKey', 'someVal'); // true or false - whether the item was added or not + + // locker.put('fooArray', ['bar', 'baz', 'bob']); + + locker.get('fooArray'); // ['bar', 'baz', 'bob'] + + locker.get('keyDoesNotExist', 'a default value'); // 'a default value' + + locker.get(['someKey', 'anotherKey', 'foo']); + /* will return something like... + { + someKey: 'someValue', + anotherKey: true, + foo: 'bar' + }*/ + + // locker.put('someKey', { foo: 'bar', baz: 'bob' }); + + locker.pull('someKey', 'defaultVal'); // { foo: 'bar', baz: 'bob' } + + // then... + + locker.get('someKey', 'defaultVal'); // 'defaultVal' + + locker.all(); + // or + locker.namespace('somethingElse').all(); + + locker.count(); + // or + locker.namespace('somethingElse').count(); + + locker.has('someKey'); // true or false + + // or + locker.namespace('foo').has('bar'); + + // e.g. + if (locker.has('user.authToken') ) { + // we're logged in + } else { + // go to login page or something + } + + locker.forget('keyToRemove'); + // or + locker.driver('session').forget('sessionKey'); + // etc.. + + locker.forget(['keyToRemove', 'anotherKeyToRemove', 'something', 'else']); + + locker.clean(); + // or + locker.namespace('someOtherNamespace').clean(); + + locker.empty(); + + locker.bind($scope, 'foo'); + $scope['foo'] = ['bar', 'baz']; + locker.get('foo'); // = ['bar', 'baz'] + + locker.bind($scope, 'foo', 'someDefault'); + $scope['foo']; // = 'someDefault' + locker.get('foo'); // = 'someDefault' + + locker.unbind($scope, 'foo'); + $scope['foo']; // = undefined + locker.get('foo'); // = undefined + + if (! locker.supported()) { + // load a polyfill? + } +}]); diff --git a/angular-locker/angular-locker.d.ts b/angular-locker/angular-locker.d.ts new file mode 100644 index 0000000000..a3cf4c87fe --- /dev/null +++ b/angular-locker/angular-locker.d.ts @@ -0,0 +1,173 @@ +// Type definitions for Angular Locker v2.0.3 +// Project: https://github.com/tymondesigns/angular-locker +// Definitions by: Niko Kovačič +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "angular-locker" { + var _: string; + export = _; +} + +declare module angular.locker { + interface ILockerServicePutFunction { + (current: any): any + } + + interface ILockerRepository { + /** + * Add an item to storage if it doesn't already exist + * + * @param {String} key The key to add + * @param {Mixed} value The value to add + */ + add(key: string, value: any): boolean; + /** + * Return all items in storage within the current namespace/driver + * + */ + all(): any; + /** + * Remove all items set within the current namespace/driver + */ + clean(): ILockerService; + /** + * Get the total number of items within the current namespace + */ + count(): number; + /** + * Retrieve the specified item from storage + * + * @param {String|Array} key The key to get + * @param {Mixed} def The default value if it does not exist + */ + get(key: string | Array, defaultValue?: any): any; + /** + * Determine whether the item exists in storage + * + * @param {String|Function} key - The key to remove + */ + has(key: string): boolean + /** + * Get the storage keys as an array + */ + keys(): Array; + /** + * Add a new item to storage (even if it already exists) + * + * @param {Object} keyValuePairs Key value object + */ + put(keyValuePairs: Object): ILockerService | boolean; + /** + * Add a new item to storage (even if it already exists) + * + * @param {Mixed} putFunction The default to pass to function if doesn't already exist + */ + put(putFunction: Function): ILockerService | boolean; + /** + * Add a new item to storage (even if it already exists) + * + * @param {Mixed} key The key to add + * @param {Mixed} value The value to add + */ + put(key: string, value: any): ILockerService | boolean; + /** + * Add a new item to storage (even if it already exists) + * + * @param {Mixed} key The key to add + * @param {Mixed} putFunction The default to pass to function if doesn't already exist + * @param {Mixed} value The value to add + */ + put(key: string, putFunction: ILockerServicePutFunction, value: any): ILockerService | boolean; + /** + * Remove specified item(s) from storage + * + * @param {String} key The key to remove + */ + forget(key: string): void; + /** + * Remove specified item(s) from storage + * + * @param {Array} keys The array of keys to remove + * + */ + forget(keys: Array): void; + /** + * Retrieve the specified item from storage and then remove it + * + * @param {String|Array} key The key to pull from storage + * @param {Mixed} def The default value if it does not exist + */ + pull(key: string | Array, defaultValue?: any): any; + } + + interface ILockerService extends ILockerRepository { + /** + * Bind a storage key to a $scope property + * + * @param {Object} $scope The angular $scope object + * @param {String} key The key in storage to bind to + * @param {Mixed} def The default value to initially bind + */ + bind(scope: IScope, property: string, defaultPropertyValue?: any): ILockerService; + /** + * Set the storage driver on a new instance to enable overriding defaults + * + * @param {String} driver The driver to switch to + */ + driver(localStorageType: string): ILockerService; + /** + * Empty the current storage driver completely. careful now. + */ + empty(): ILockerService; + /** + * Get the currently set namespace + */ + getNamespace(): string; + /** + * Get a new instance of Locker + * + * @param {Object} options The config options to instantiate with + */ + instance(lockerSettings: ILockerSettings): ILockerService; + /** + * Set the namespace on a new instance to enable overriding defaults + * + * @param {String} namespace The namespace to switch to + */ + 'namespace'(name: string): ILockerRepository; + /** + * Check browser support + * + * @see github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js#L38-L47 + * + * @param {String} driver The driver to check support with + */ + supported(): boolean; + /** + * Unbind a storage key from a $scope property + * + * @param {Object} $scope The angular $scope object + * @param {String} key The key to remove from bindings + */ + unbind(scope: IScope, property: string): void; + } + + interface ILockerSettings { + driver?: string; + 'namespace'?: string | boolean; + separator?: string; + eventsEnabled?: boolean; + extend?: any; + } + + interface ILockerProvider extends angular.IServiceProvider { + /** + * Allow the defaults to be specified via the `lockerProvider` + * + * @param {ILockerSettings} lockerSettings The defaults to override + */ + defaults(lockerSettings: ILockerSettings): void; + } +} \ No newline at end of file