-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
283 lines (256 loc) · 6.01 KB
/
utils.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const { format, formatRelative } = require('date-fns')
const enums = {
jobType: {
print: [
'Postcard',
'Tri-fold service',
'Tri-fold offer sales',
'Invoice w/ voucher buy back',
'Invoice w/ck',
'Invoice bilingual w/voucher',
'Email buy back',
'Letter orignal',
'Letter w/voucher w/offers',
'Letter certificate',
'Letter tax double window bilingual',
'Letter w/offers buy back',
'Check stub w/voucher',
'Carbon',
'Tax snap buy back'
],
digital: [
'Website Sliders',
'Google Banner',
'Email Blast',
'Service',
'Facebook',
'Instagram',
'Billboards',
'Service Slide',
'Service Coupon'
]
},
addons: [
'Scratcher',
'Key',
'Hologram Scratcher',
'KBB Append',
'Handwriting',
'4x6 Note',
'Lamination',
'Pull Tab',
'Post-It-Note',
'Bus. Card 1-sided',
'BB Append',
'Bus. Card 2-sided',
'Credit Card',
'Spanish',
'Custom'
],
listType: ['Database', 'Saturation', 'Bankruptcy', 'Prequalified'],
postage: [
'Indicia, Standard',
'Indicia, 1st Class',
'Stamp, Standard',
'Stamp, 1st Class'
],
envelope: [
'None',
'#10',
'#10 Windowed',
'#10 Full-Windowed',
'6 x 9',
'6 x 9 Windowed',
'6 x 9 Double-Windowed'
],
size: {
print: ['8.5 x 11', '8.5 x 14', '9 x 7', '11 x 15', '11 x 17'],
digital: [
'1920 x 1080',
'1920 x 850',
'1920 x 650',
'1920 x 300',
'1600 x 514',
'1080 x 1350',
'575 x 375',
'464 x 240',
'336 x 280',
'320 x 100',
'300 x 250',
'160 x 600'
]
},
artStatus: [
'Unassigned',
'Info Needed',
'In Progress',
'Art Review',
'Needs Changes',
'Art Approved',
'Client Review',
'Client Approved',
'Compliance Review',
'Compliance Approved',
'Approved to Post',
'Printer Proof',
'Complete'
],
eventTypes: ['created', 'changed']
}
enums.jobType.all = [...enums.jobType.print, ...enums.jobType.digital]
function colorize(status) {
switch (status) {
case 'List Uploaded':
case 'List Pending':
case 'Complete':
return 'green'
case 'Count Pending':
case 'In Progress':
return 'yellow'
case 'Incomplete':
case 'Unassigned':
return 'red'
default:
return undefined
}
}
function formatNumber(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
function formatPhone(phone) {
return phone
? `+1 (${phone.substring(0, 3)}) ${phone.substring(3, 6)} ${phone.substring(
6
)}`
: ''
}
function formatDate(date) {
return format(date, 'MM/dd/yyyy')
}
function formatDateTime(date) {
return formatRelative(date, new Date())
}
const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/
function parseJSON(json) {
return JSON.parse(json, (property, value) =>
typeof value == 'string' && dateRegex.test(value) ? new Date(value) : value
)
}
function clone(obj) {
return parseJSON(JSON.stringify(obj))
}
function transformValue(obj, func) {
return Array.isArray(obj)
? obj.map(value => transformValue(value, func))
: typeof obj == 'object' && obj !== null
? Object.entries(obj).reduce(
(o, [key, value]) => ((o[key] = transformValue(value, func)), o),
{}
)
: func(obj)
}
function range(start, end) {
var total = []
if (!end) {
end = start
start = 0
}
for (var i = start; i < end; i += 1) {
total.push(i)
}
return total
}
/**
* Simple object check.
* @param item
* @returns {boolean}
*/
function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item)
}
/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
function deepmerge(target, ...sources) {
if (!sources.length) return target
const source = sources.shift()
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
deepmerge(target[key], source[key])
} else {
Object.assign(target, { [key]: source[key] })
}
}
}
return deepmerge(target, ...sources)
}
/**
* Returns a function, that, when invoked, will only be triggered at most once
* during a given window of time. Normally, the throttled function will run
* as much as it can, without ever going more than once per `wait` duration;
* but if you'd like to disable the execution on the leading edge, pass
*`{leading: false}`. To disable execution on the trailing edge, ditto.
*/
function throttle(func, wait, options) {
var timeout, context, args, result
var previous = 0
if (!options) options = {}
var later = function() {
previous = options.leading === false ? 0 : Date.now
timeout = null
result = func.apply(context, args)
if (!timeout) context = args = null
}
var throttled = function() {
var now = Date.now
if (!previous && options.leading === false) previous = now
var remaining = wait - (now - previous)
context = this
args = arguments
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
previous = now
result = func.apply(context, args)
if (!timeout) context = args = null
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining)
}
return result
}
throttled.cancel = function() {
clearTimeout(timeout)
previous = 0
timeout = context = args = null
}
return throttled
}
function ensureArray(value) {
return value === undefined ? [] : Array.isArray(value) ? value : [value]
}
function basename(file) {
return decodeURIComponent(file.substring(file.lastIndexOf('/') + 1))
}
module.exports = {
enums,
colorize,
formatNumber,
formatPhone,
formatDate,
formatDateTime,
parseJSON,
clone,
transformValue,
range,
isObject,
deepmerge,
throttle,
ensureArray,
basename
}