@drawbridge/drawbridge-utils 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,410 @@
1
+ import { data, code } from 'currency-codes';
2
+
3
+ const font = {
4
+ family : 'Roboto Flex',
5
+ transform : 'none',
6
+ weight : 'regular'
7
+ };
8
+
9
+ var constantsData = {
10
+ action : {
11
+ usage : {
12
+ actions : 0
13
+ }
14
+ },
15
+ brand : {
16
+ style : {
17
+ body : font,
18
+ heading : font
19
+ },
20
+ totals : {
21
+ campaigns : 0
22
+ }
23
+ },
24
+ campaign : {
25
+ agreements : {
26
+ marketing : {
27
+ enabled : false,
28
+ label : 'I agree to the marketing terms and conditions',
29
+ link : null
30
+ },
31
+ terms : {
32
+ enabled : false,
33
+ label : 'I agree to the terms and conditions',
34
+ link : null
35
+ }
36
+ },
37
+ defaults : {
38
+ active : 'Enter',
39
+ confirmation : 'Submission was successful',
40
+ inactive : 'Submissions are closed',
41
+ email : 'You were selected'
42
+ },
43
+ notifications : {
44
+ draw : {
45
+ subject : 'You have been selected',
46
+ body : 'Thanks for being part of our giveaway'
47
+ }
48
+ },
49
+ settings : {
50
+ submissionLeadPrimaryKey : 'email',
51
+ submissionEntryMaximum : 1,
52
+ submissionEntryFilter : 'campaign',
53
+ submissionEntryHighscore : false,
54
+ submissionEntryLimit : 1
55
+ },
56
+ status : 'drafted',
57
+ totals : {
58
+ advertisements : 0,
59
+ affiliates : 0,
60
+ draws : 0,
61
+ exports : 0,
62
+ entries : 0,
63
+ fields : 2,
64
+ integrations : 1,
65
+ leads : 0,
66
+ links : 0,
67
+ pages : 0,
68
+ prizes : 0,
69
+ ranges : 0,
70
+ submissions : 0
71
+ },
72
+ type : 'giveaway'
73
+ },
74
+ draw : {
75
+ status : 'qualified',
76
+ totals : {
77
+ notifications : 0
78
+ }
79
+ },
80
+ member : {
81
+ status : 'pending'
82
+ },
83
+ organization : {
84
+ errors : [],
85
+ totals : {
86
+ affiliates : 0,
87
+ brands : 0,
88
+ campaigns : 0,
89
+ leads : 0,
90
+ draws : 0,
91
+ entries : 0,
92
+ invitations : 0,
93
+ invoices : 0,
94
+ members : 0,
95
+ ranges : 0,
96
+ pages : 0,
97
+ prizes : 0,
98
+ storage : 0,
99
+ submissions : 0,
100
+ subscriptions : 0,
101
+ usage : 0
102
+ }
103
+ },
104
+ page : {
105
+ domains : [],
106
+ status : 'drafted',
107
+ totals : {
108
+ leads : 0,
109
+ entries : 0,
110
+ submissions : 0
111
+ }
112
+ },
113
+ prize : {
114
+ inventory : 0,
115
+ remaining : 0,
116
+ shipping : false,
117
+ status : 'drafted',
118
+ totals : {
119
+ draws : 0
120
+ }
121
+ },
122
+ range : {
123
+ totals : {
124
+ leads : 0,
125
+ entries : 0,
126
+ submissions : 0
127
+ }
128
+ },
129
+ referrer : {
130
+ totals : {
131
+ originizations : 0,
132
+ subscriptions : 0,
133
+ users : 0
134
+ }
135
+ },
136
+ template : {
137
+ page : {
138
+ style : {
139
+ body : font,
140
+ heading : font
141
+ }
142
+ }
143
+ },
144
+ usage : {
145
+ totals : {
146
+ actions : 0,
147
+ affiliates : 0,
148
+ brands : 0,
149
+ campaigns : 0,
150
+ connections : 0,
151
+ entries : 0,
152
+ files : 0,
153
+ members : 0,
154
+ orders : 0,
155
+ pages : 0,
156
+ revenue : 0,
157
+ storage : 0,
158
+ submissions : 0,
159
+ workflows : 0
160
+ }
161
+ },
162
+ user : {
163
+ access : {
164
+ ai : false
165
+ },
166
+ image : null,
167
+ totals : {
168
+ inbox : 0,
169
+ invitations : 0,
170
+ messages : 0,
171
+ methods : 0,
172
+ organizations : 1,
173
+ teams : 0
174
+ }
175
+ }
176
+ };
177
+
178
+ const constants = constantsData;
179
+
180
+ const infinite = 1e300;
181
+
182
+ const isInfinite = ( value ) => value === infinite;
183
+
184
+ const megabyte = ( 1024 * 1024 );
185
+ const gigabyte = ( megabyte * 1024 );
186
+
187
+ const urlRoot = ( string ) => ( string?.includes( 'https' ) ? 'https://' : 'http://' );
188
+
189
+ const bytesToGB = ( bytes ) => {
190
+
191
+ const divide = ( 1024 * 1024 * 1024 );
192
+
193
+ return bytes ? Number( bytes ) / divide : 0;
194
+
195
+ };
196
+
197
+ const bytesToMB = ( bytes ) => {
198
+
199
+ const divide = ( 1024 * 1024 );
200
+
201
+ return bytes ? Number( bytes ) / divide : 0;
202
+
203
+ };
204
+
205
+ const capitalize = ( string ) => {
206
+
207
+ return string?.charAt( 0 )?.toUpperCase() + string?.slice( 1 );
208
+
209
+ };
210
+
211
+ const expiredPaymentMethod = ( card ) => {
212
+
213
+ const currentDate = new Date();
214
+ const year = currentDate.getFullYear();
215
+ const month = new Date().getMonth();
216
+
217
+ const expired = [
218
+ card?.year < year,
219
+ card?.year === year && card?.month <= month
220
+ ].filter( Boolean );
221
+
222
+ return Boolean( expired?.length );
223
+
224
+ };
225
+
226
+ const formatCurrency = ({
227
+ code = 'USD',
228
+ digits = 2,
229
+ display = 'standard',
230
+ locale,
231
+ value = 0
232
+ }) => {
233
+
234
+ const resolvedLocale = locale
235
+ || ( typeof navigator !== 'undefined' ? navigator?.language : null )
236
+ || 'en-US';
237
+
238
+ const options = {
239
+ currency : code,
240
+ minimumFractionDigits : digits,
241
+ maximumFractionDigits : digits,
242
+ style : 'currency'
243
+ };
244
+
245
+ if( display === 'parts' ){
246
+
247
+ options.currencyDisplay = 'narrowSymbol';
248
+
249
+ const parts = new Intl.NumberFormat( resolvedLocale, options ).formatToParts( value );
250
+
251
+ const symbol = parts.find( ( p ) => p.type === 'currency' )?.value || '';
252
+ const number = parts
253
+ .filter( ( p ) => p.type !== 'currency' && p.type !== 'literal' )
254
+ .map( ( p ) => p.value )
255
+ .join( '' );
256
+
257
+ return symbol + number + ' ' + code;
258
+
259
+ }
260
+ return new Intl.NumberFormat( resolvedLocale, options ).format( value );
261
+
262
+ };
263
+
264
+ const formatDateString = ({
265
+ date,
266
+ locale,
267
+ time = false,
268
+ options = {
269
+ day : 'numeric',
270
+ month : 'long',
271
+ weekday : 'long',
272
+ year : 'numeric'
273
+ }
274
+ }) => {
275
+
276
+ const resolved = time
277
+ ? { ...options, hour : 'numeric', minute : '2-digit' }
278
+ : options;
279
+
280
+ return new Date( date ).toLocaleString(
281
+ locale,
282
+ resolved
283
+ );
284
+
285
+ };
286
+
287
+ const formatNumber = (
288
+ number,
289
+ digits = 0
290
+ ) => {
291
+
292
+ return Number( number ).toLocaleString( 'en', {
293
+ minimumFractionDigits : digits,
294
+ maximumFractionDigits : digits
295
+ });
296
+
297
+ };
298
+
299
+ const getPlanFeature = ( plan, key ) => {
300
+
301
+ const error = ( plan?.features?.denied || {} )?.[ key ];
302
+ const feature = ( plan?.features?.granted || {} )?.[ key ];
303
+ const granted = Boolean( feature );
304
+
305
+ return {
306
+ granted,
307
+ message : granted ? feature : error
308
+ };
309
+
310
+ };
311
+
312
+ const percentage = ( value1, value2, decimals = 1 ) => ( ( ( ( value1 || 0 ) / ( value2 || 0 ) ) * 100 ) || 0 ).toFixed( decimals );
313
+
314
+ const reducers = {
315
+ fields : ( data = [], callback = () => ({}) ) => data.reduce(
316
+ ( accumulator, field ) => {
317
+
318
+ const type = field?.type;
319
+
320
+ if( ! accumulator[ type ] ) return accumulator;
321
+
322
+ accumulator[ type ].items.push({
323
+ ...field,
324
+ ...callback( field )
325
+ });
326
+
327
+ accumulator[ type ].keys.push( field?.slug );
328
+
329
+ return accumulator;
330
+
331
+ },
332
+ {
333
+ additional : {
334
+ items : [],
335
+ keys : []
336
+ },
337
+ lead : {
338
+ items : [],
339
+ keys : []
340
+ }
341
+ }
342
+ )
343
+ };
344
+
345
+ const regex = {
346
+ domain : /^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/,
347
+ protocols : /^(https?:\/\/)?(www\.)?/,
348
+ url : /^(https:\/\/)/
349
+ };
350
+
351
+ const shareUrls = ({
352
+ formUri,
353
+ organization,
354
+ page
355
+ }) => {
356
+
357
+ const resolvedFormUri = formUri
358
+ || ( typeof process !== 'undefined' ? process.env?.APP_CLIENT_FORM_URI : undefined );
359
+
360
+ const preface = urlRoot( resolvedFormUri );
361
+ const base = resolvedFormUri;
362
+
363
+ const qr = preface + base?.replace( preface, '' ) + '/' + page?.shortId + '?qr=1';
364
+ const short = preface + base?.replace( preface, '' ) + '/' + page?.shortId;
365
+ const url = preface + organization?.subdomain + '.' + base?.replace( preface, '' ) + '/' + page?.slug;
366
+
367
+ const clients = [
368
+ base,
369
+ qr,
370
+ short,
371
+ url
372
+ ];
373
+
374
+ const urls = {
375
+ qr,
376
+ short,
377
+ url
378
+ };
379
+
380
+ if( organization?.subdomain && page?.shortId ){
381
+
382
+ clients.push( preface + base?.replace( preface, '' ) + '/' + page?.shortId );
383
+ clients.push( preface + base?.replace( preface, '' ) + '/' + page?.shortId + '?qr=1' );
384
+ clients.push( preface + organization.subdomain + '.' + base?.replace( preface, '' ) );
385
+ clients.push( preface + organization.subdomain + '.' + base?.replace( preface, '' ) + '/' + page?.shortId );
386
+
387
+ }
388
+ if( organization?.shortId && page?.slug && page?.shortId ){
389
+
390
+ clients.push( preface + organization.shortId + '.' + base?.replace( preface, '' ) );
391
+ clients.push( preface + organization.shortId + '.' + base?.replace( preface, '' ) + '/' + page?.slug );
392
+ clients.push( preface + organization.shortId + '.' + base?.replace( preface, '' ) + '/' + page?.shortId );
393
+
394
+ }
395
+ return {
396
+ clients,
397
+ urls
398
+ };
399
+
400
+ };
401
+
402
+ const currencies = data.map( ( item ) => ({
403
+ ...item,
404
+ key : item.currency,
405
+ value : item.code
406
+ }));
407
+
408
+ const currency = ( val ) => code( val );
409
+
410
+ export { bytesToGB, bytesToMB, capitalize, constants, currencies, currency, expiredPaymentMethod, formatCurrency, formatDateString, formatNumber, getPlanFeature, gigabyte, infinite, isInfinite, megabyte, percentage, reducers, regex, shareUrls, urlRoot };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,182 @@
1
+ import { data, code } from 'currency-codes';
2
+
3
+ const font = {
4
+ family : 'Roboto Flex',
5
+ transform : 'none',
6
+ weight : 'regular'
7
+ };
8
+
9
+ var constantsData = {
10
+ action : {
11
+ usage : {
12
+ actions : 0
13
+ }
14
+ },
15
+ brand : {
16
+ style : {
17
+ body : font,
18
+ heading : font
19
+ },
20
+ totals : {
21
+ campaigns : 0
22
+ }
23
+ },
24
+ campaign : {
25
+ agreements : {
26
+ marketing : {
27
+ enabled : false,
28
+ label : 'I agree to the marketing terms and conditions',
29
+ link : null
30
+ },
31
+ terms : {
32
+ enabled : false,
33
+ label : 'I agree to the terms and conditions',
34
+ link : null
35
+ }
36
+ },
37
+ defaults : {
38
+ active : 'Enter',
39
+ confirmation : 'Submission was successful',
40
+ inactive : 'Submissions are closed',
41
+ email : 'You were selected'
42
+ },
43
+ notifications : {
44
+ draw : {
45
+ subject : 'You have been selected',
46
+ body : 'Thanks for being part of our giveaway'
47
+ }
48
+ },
49
+ settings : {
50
+ submissionLeadPrimaryKey : 'email',
51
+ submissionEntryMaximum : 1,
52
+ submissionEntryFilter : 'campaign',
53
+ submissionEntryHighscore : false,
54
+ submissionEntryLimit : 1
55
+ },
56
+ status : 'drafted',
57
+ totals : {
58
+ advertisements : 0,
59
+ affiliates : 0,
60
+ draws : 0,
61
+ exports : 0,
62
+ entries : 0,
63
+ fields : 2,
64
+ integrations : 1,
65
+ leads : 0,
66
+ links : 0,
67
+ pages : 0,
68
+ prizes : 0,
69
+ ranges : 0,
70
+ submissions : 0
71
+ },
72
+ type : 'giveaway'
73
+ },
74
+ draw : {
75
+ status : 'qualified',
76
+ totals : {
77
+ notifications : 0
78
+ }
79
+ },
80
+ member : {
81
+ status : 'pending'
82
+ },
83
+ organization : {
84
+ errors : [],
85
+ totals : {
86
+ affiliates : 0,
87
+ brands : 0,
88
+ campaigns : 0,
89
+ leads : 0,
90
+ draws : 0,
91
+ entries : 0,
92
+ invitations : 0,
93
+ invoices : 0,
94
+ members : 0,
95
+ ranges : 0,
96
+ pages : 0,
97
+ prizes : 0,
98
+ storage : 0,
99
+ submissions : 0,
100
+ subscriptions : 0,
101
+ usage : 0
102
+ }
103
+ },
104
+ page : {
105
+ domains : [],
106
+ status : 'drafted',
107
+ totals : {
108
+ leads : 0,
109
+ entries : 0,
110
+ submissions : 0
111
+ }
112
+ },
113
+ prize : {
114
+ inventory : 0,
115
+ remaining : 0,
116
+ shipping : false,
117
+ status : 'drafted',
118
+ totals : {
119
+ draws : 0
120
+ }
121
+ },
122
+ range : {
123
+ totals : {
124
+ leads : 0,
125
+ entries : 0,
126
+ submissions : 0
127
+ }
128
+ },
129
+ referrer : {
130
+ totals : {
131
+ originizations : 0,
132
+ subscriptions : 0,
133
+ users : 0
134
+ }
135
+ },
136
+ template : {
137
+ page : {
138
+ style : {
139
+ body : font,
140
+ heading : font
141
+ }
142
+ }
143
+ },
144
+ usage : {
145
+ totals : {
146
+ actions : 0,
147
+ affiliates : 0,
148
+ brands : 0,
149
+ campaigns : 0,
150
+ connections : 0,
151
+ entries : 0,
152
+ files : 0,
153
+ members : 0,
154
+ orders : 0,
155
+ pages : 0,
156
+ revenue : 0,
157
+ storage : 0,
158
+ submissions : 0,
159
+ workflows : 0
160
+ }
161
+ },
162
+ user : {
163
+ access : {
164
+ ai : false
165
+ },
166
+ image : null,
167
+ totals : {
168
+ inbox : 0,
169
+ invitations : 0,
170
+ messages : 0,
171
+ methods : 0,
172
+ organizations : 1,
173
+ teams : 0
174
+ }
175
+ }
176
+ };
177
+
178
+ const constants = constantsData;
179
+
1
180
  const infinite = 1e300;
2
181
 
3
182
  const isInfinite = ( value ) => value === infinite;
@@ -7,12 +186,43 @@ const gigabyte = ( megabyte * 1024 );
7
186
 
8
187
  const urlRoot = ( string ) => ( string?.includes( 'https' ) ? 'https://' : 'http://' );
9
188
 
189
+ const bytesToGB = ( bytes ) => {
190
+
191
+ const divide = ( 1024 * 1024 * 1024 );
192
+
193
+ return bytes ? Number( bytes ) / divide : 0;
194
+
195
+ };
196
+
197
+ const bytesToMB = ( bytes ) => {
198
+
199
+ const divide = ( 1024 * 1024 );
200
+
201
+ return bytes ? Number( bytes ) / divide : 0;
202
+
203
+ };
204
+
10
205
  const capitalize = ( string ) => {
11
206
 
12
207
  return string?.charAt( 0 )?.toUpperCase() + string?.slice( 1 );
13
208
 
14
209
  };
15
210
 
211
+ const expiredPaymentMethod = ( card ) => {
212
+
213
+ const currentDate = new Date();
214
+ const year = currentDate.getFullYear();
215
+ const month = new Date().getMonth();
216
+
217
+ const expired = [
218
+ card?.year < year,
219
+ card?.year === year && card?.month <= month
220
+ ].filter( Boolean );
221
+
222
+ return Boolean( expired?.length );
223
+
224
+ };
225
+
16
226
  const formatCurrency = ({
17
227
  code = 'USD',
18
228
  digits = 2,
@@ -99,6 +309,39 @@ const getPlanFeature = ( plan, key ) => {
99
309
 
100
310
  };
101
311
 
312
+ const percentage = ( value1, value2, decimals = 1 ) => ( ( ( ( value1 || 0 ) / ( value2 || 0 ) ) * 100 ) || 0 ).toFixed( decimals );
313
+
314
+ const reducers = {
315
+ fields : ( data = [], callback = () => ({}) ) => data.reduce(
316
+ ( accumulator, field ) => {
317
+
318
+ const type = field?.type;
319
+
320
+ if( ! accumulator[ type ] ) return accumulator;
321
+
322
+ accumulator[ type ].items.push({
323
+ ...field,
324
+ ...callback( field )
325
+ });
326
+
327
+ accumulator[ type ].keys.push( field?.slug );
328
+
329
+ return accumulator;
330
+
331
+ },
332
+ {
333
+ additional : {
334
+ items : [],
335
+ keys : []
336
+ },
337
+ lead : {
338
+ items : [],
339
+ keys : []
340
+ }
341
+ }
342
+ )
343
+ };
344
+
102
345
  const regex = {
103
346
  domain : /^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/,
104
347
  protocols : /^(https?:\/\/)?(www\.)?/,
@@ -111,8 +354,11 @@ const shareUrls = ({
111
354
  page
112
355
  }) => {
113
356
 
114
- const preface = urlRoot( formUri );
115
- const base = formUri;
357
+ const resolvedFormUri = formUri
358
+ || ( typeof process !== 'undefined' ? process.env?.APP_CLIENT_FORM_URI : undefined );
359
+
360
+ const preface = urlRoot( resolvedFormUri );
361
+ const base = resolvedFormUri;
116
362
 
117
363
  const qr = preface + base?.replace( preface, '' ) + '/' + page?.shortId + '?qr=1';
118
364
  const short = preface + base?.replace( preface, '' ) + '/' + page?.shortId;
@@ -153,17 +399,12 @@ const shareUrls = ({
153
399
 
154
400
  };
155
401
 
156
- module.exports = {
157
- capitalize,
158
- formatCurrency,
159
- formatDateString,
160
- formatNumber,
161
- getPlanFeature,
162
- gigabyte,
163
- infinite,
164
- isInfinite,
165
- megabyte,
166
- regex,
167
- shareUrls,
168
- urlRoot
169
- };
402
+ const currencies = data.map( ( item ) => ({
403
+ ...item,
404
+ key : item.currency,
405
+ value : item.code
406
+ }));
407
+
408
+ const currency = ( val ) => code( val );
409
+
410
+ export { bytesToGB, bytesToMB, capitalize, constants, currencies, currency, expiredPaymentMethod, formatCurrency, formatDateString, formatNumber, getPlanFeature, gigabyte, infinite, isInfinite, megabyte, percentage, reducers, regex, shareUrls, urlRoot };