forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-locker-tests.ts
132 lines (98 loc) · 3.59 KB
/
angular-locker-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import * as angular from 'angular';
angular
.module('angular-locker-tests', ['angular-locker'])
.config(['lockerProvider', function config(lockerProvider: angular.locker.ILockerProvider) {
let lockerSettings: angular.locker.ILockerSettings = {
driver: 'session',
namespace: 'myApp',
separator: '.',
eventsEnabled: true,
extend: <any>{}
};
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: any) {
current.push('baz');
return current;
});
locker.get('someKey'); // = ['foo', 'bar', 'baz']
// given locker.get('foo') is not defined
locker.put('foo', function (current: any) {
// 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?
}
}]);