forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore.js
More file actions
145 lines (133 loc) · 2.88 KB
/
Store.js
File metadata and controls
145 lines (133 loc) · 2.88 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict'
/**
* adonis-framework
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const Type = require('type-of-is')
const _ = require('lodash')
/**
* Session values store to guard and unguard
* values.
*
* @exports SessionStore
*/
const Store = exports = module.exports = {}
/**
* Guarding a key/value pair inside an object
* by storing the original data type.
*
* @param {String} key
* @param {Mixed} value
*
* @invalidTypes - Function, RegExp, Error
*
* @return {Object}
*
* @example
* Store.guardPair('name', 'Foo') => {name: {d: 'Foo', t: 'String'}}
*/
Store.guardPair = function (key, value) {
const type = Type.string(value)
switch (type) {
case 'Number':
value = String(value)
break
case 'Object':
value = JSON.stringify(value)
break
case 'Array':
value = JSON.stringify(value)
break
case 'Boolean':
value = String(value)
break
case 'Function':
value = null
break
case 'RegExp':
value = null
break
case 'Date':
value = String(value)
break
case 'Error':
value = null
break
}
if (!value) {
return value
}
return {d: value, t: type}
}
/**
* Unguards a pair which was guarded earlier and
* returns the original value with correct
* data type.
*
* @param {Object} pair
*
* @return {Mixed}
*
* @example
* Store.unGuardPair({name: {d: 'Foo', t: 'String'}}) => {name: 'Foo'}
*
* @throws {InvalidArgumentException} If pair does not have d & t properties
*/
Store.unGuardPair = function (pair) {
if (!pair || !pair.d || !pair.t) {
throw new Error('Cannot unguard unrecognized pair type')
}
/** if parsed do not re parse */
if (typeof (pair.d) === pair.t.toLowerCase()) {
return pair.d
}
switch (pair.t) {
case 'Number':
pair.d = Number(pair.d)
break
case 'Object':
try { pair.d = JSON.parse(pair.d) } catch (e) {}
break
case 'Array':
try { pair.d = JSON.parse(pair.d) } catch (e) {}
break
case 'Boolean':
pair.d = pair.d === 'true' || pair.d === '1'
break
}
return pair.d
}
/**
* Pack values from a plain object to an object to be
* saved as JSON.stringfied string
*
* @param {Object} values
*
* @return {Object}
*/
Store.packValues = function (values) {
return _.transform(values, (result, value, key) => {
const body = Store.guardPair(key, value)
if (body) {
result[key] = body
}
return result
}, {})
}
/**
* Unpack values from store to a normal object
*
* @param {Object} values
*
* @return {Object}
*/
Store.unPackValues = function (values) {
return _.transform(values, (result, value, index) => {
result[index] = Store.unGuardPair(value)
return result
}, {}) || {}
}