@qikdev/sdk 0.1.14 → 0.1.18

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.
@@ -1,17 +1,565 @@
1
- import _ from 'lodash';
1
+ import _get from 'lodash/get';
2
+ import _isDate from 'lodash/isDate';
3
+ import _startsWith from 'lodash/startsWith';
4
+ import _isObject from 'lodash/isObject';
5
+ import _camelCase from 'lodash/camelCase';
2
6
  import axios from 'axios';
3
7
  import { isBrowser, isNode } from 'browser-or-node';
4
8
 
5
9
 
10
+
6
11
  ///////////////////////////////////////////////////////////////////////////////
7
12
 
8
- /**
9
- * @classdesc A static service that provides useful helper functions and tools for other Qik services
10
- * @alias utils
11
- * @class
12
- * @hideconstructor
13
- */
14
- var QikUtils = {};
13
+ var service = {};
14
+
15
+
16
+ ///////////////////////////////////////////////////////////////////////////////
17
+
18
+
19
+ service.exists = function(value) {
20
+ var isUndefinedOrNull;
21
+
22
+ ////////////////////////
23
+
24
+ if (Array.isArray(value)) {
25
+ return true;
26
+ }
27
+
28
+ ////////////////////////
29
+
30
+ if (value === 0) {
31
+ return true;
32
+ }
33
+
34
+ ////////////////////////
35
+
36
+ switch (typeof value) {
37
+ case 'undefined':
38
+ case 'null':
39
+ isUndefinedOrNull = true;
40
+ break;
41
+ default:
42
+ var string = String(value).toLowerCase();
43
+ switch (string) {
44
+ case '':
45
+ case 'undefined':
46
+ case 'null':
47
+ isUndefinedOrNull = true;
48
+ break;
49
+ }
50
+ break;
51
+ }
52
+
53
+ return !!!isUndefinedOrNull;
54
+ }
55
+
56
+ //////////////////////////////////
57
+
58
+ service.parseDate = function(input) {
59
+
60
+ if (!input) {
61
+ return;
62
+ }
63
+
64
+ if (_isDate(input)) {
65
+ return input;
66
+ }
67
+
68
+ //Attempt to create a date
69
+ var d = new Date(input);
70
+ var isValid = d instanceof Date && !isNaN(d);
71
+
72
+ if (isValid) {
73
+ return d;
74
+ }
75
+
76
+ return;
77
+ }
78
+
79
+ service.parseURL = function(string) {
80
+
81
+ if (!string) {
82
+ return false;
83
+ }
84
+
85
+ if (_startsWith(string, '/')) {
86
+ return string;
87
+ }
88
+
89
+ if (_startsWith(string, '://')) {
90
+ return string;
91
+ }
92
+
93
+ if (_startsWith(string, 'mailto:')) {
94
+ return string;
95
+ }
96
+
97
+ if (_startsWith(string, 'tel:')) {
98
+ return string;
99
+ }
100
+
101
+ if (_startsWith(string, 'sms:')) {
102
+ return string;
103
+ }
104
+
105
+ //////////////////////////////////
106
+ //If someone entered an email by accident
107
+ var email = service.parseEmail(string);
108
+ //Convert it to a mailto link
109
+ if (email && email === string) {
110
+ return `mailto:${email}`
111
+ }
112
+
113
+ //////////////////////////////////
114
+
115
+ const withHttp = string => !/^https?:\/\//i.test(string) ? `http://${string}` : string;
116
+ string = withHttp(string);
117
+
118
+
119
+
120
+ var valid = RegExp('(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+@]*)*(\\?[;&a-z\\d%_.~+=-@]*)?(\\#[-a-z\\d_@]*)?$', 'i').test(string);
121
+
122
+ if (!valid) {
123
+ return false;
124
+ }
125
+
126
+
127
+
128
+ ///////////////////////////
129
+
130
+ return withHttp(string)
131
+ }
132
+
133
+
134
+ ///////////////////////////////////////////////
135
+
136
+ service.parseNumber = function(input, decimalPoints) {
137
+ if (!input) {
138
+ return 0;
139
+ }
140
+
141
+ input = Number(input);
142
+ if (isNaN(input)) {
143
+ return 0;
144
+ }
145
+
146
+ if (decimalPoints) {
147
+ var str = input.toFixed(decimalPoints);
148
+ input = service.parseNumber(str);
149
+ }
150
+
151
+ return input;
152
+ }
153
+
154
+
155
+ service.isValidEmailAddress = function(email) {
156
+
157
+ var tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;
158
+
159
+ if (!email) return false;
160
+
161
+ var emailParts = email.split('@');
162
+
163
+ if (emailParts.length !== 2) return false
164
+
165
+ var account = emailParts[0];
166
+ var address = emailParts[1];
167
+
168
+ if (account.length > 64) return false
169
+
170
+ else if (address.length > 255) return false
171
+
172
+ var domainParts = address.split('.');
173
+ if (domainParts.some(function(part) {
174
+ return part.length > 63;
175
+ })) return false;
176
+
177
+
178
+ if (!tester.test(email)) return false;
179
+
180
+ return true;
181
+ }
182
+
183
+
184
+ service.parseEmail = function(input) {
185
+
186
+ if (!input) {
187
+ return false;
188
+ }
189
+
190
+ var lowercase = String(input).toLowerCase();
191
+ var valid = service.isValidEmailAddress(lowercase);
192
+
193
+ if (!valid) {
194
+ return false;
195
+ } else {
196
+ return lowercase;
197
+ }
198
+ }
199
+
200
+ //////////////////////////////////
201
+
202
+ service.parseInt = function(input) {
203
+ if (!input) {
204
+ return 0;
205
+ }
206
+
207
+ input = parseInt(input);
208
+ if (isNaN(input)) {
209
+ return 0;
210
+ }
211
+
212
+ return input;
213
+ }
214
+
215
+
216
+
217
+ service.cleanValue = function(data, type, options) {
218
+
219
+ if (!options) {
220
+ options = {};
221
+ }
222
+
223
+ ////////////////////////
224
+
225
+ var noInputValue = service.exists(data);
226
+ if (options.strict && noInputValue && type != 'boolean') {
227
+ return data;
228
+ }
229
+
230
+ ////////////////////////
231
+
232
+ switch (type) {
233
+ case 'reference':
234
+ return service.id(data, true);
235
+ break;
236
+ case 'boolean':
237
+ return service.parseBoolean(data);
238
+ break;
239
+ case 'url':
240
+ return service.parseURL(data);
241
+ break;
242
+ case 'key':
243
+ return service.machineName(data);
244
+ break;
245
+ case 'email':
246
+ return service.parseEmail(data);
247
+ break;
248
+ case 'date':
249
+ var parsed = service.parseDate(data, options);
250
+
251
+ if (parsed === undefined) {
252
+ return;
253
+ } else {
254
+ return parsed;
255
+ }
256
+ break;
257
+ case 'number':
258
+ case 'decimal':
259
+ case 'float':
260
+ var val = service.parseNumber(data);
261
+ if (String(val) == String(Number(data))) {
262
+ return val;
263
+ }
264
+ return;
265
+ break;
266
+ case 'integer':
267
+ var val = service.parseInt(data);
268
+ if (String(val) == String(parseInt(data))) {
269
+ return val;
270
+ }
271
+ return;
272
+ break;
273
+ case 'group':
274
+ return data;
275
+ break;
276
+ case 'object':
277
+ if (_isObject(data) && !Array.isArray(data)) {
278
+ return data
279
+ }
280
+ return;
281
+ break;
282
+ case 'string':
283
+
284
+ if (service.exists(data)) {
285
+ var stringed = String(data);
286
+ if (stringed == '[object Object]') {
287
+ return;
288
+ }
289
+
290
+ return stringed
291
+ } else {
292
+ return;
293
+ }
294
+
295
+ // if(!data) {
296
+ // return '';
297
+ // }
298
+
299
+ // if(Array.isArray(data)) {
300
+ // return JSON.stringify(data);
301
+ // }
302
+
303
+ // if(Object.isObject(data)) {
304
+ // return JSON.stringify(data);
305
+ // }
306
+
307
+ // return String(data);
308
+
309
+ // break;
310
+ default:
311
+ return String(data)
312
+ break;
313
+ }
314
+ }
315
+
316
+
317
+
318
+ service.isValidValue = function(value, dataType, strict) {
319
+
320
+ var isValue = service.exists(value);
321
+ var valueIsNumber = (typeof value == 'number')
322
+
323
+ ////////////////////////
324
+
325
+ var isValidEntry = false;
326
+
327
+ if (!isValue) {
328
+ return false;
329
+ }
330
+
331
+ ////////////////////////
332
+
333
+ switch (dataType) {
334
+
335
+ case 'url':
336
+ if (strict) {
337
+ isValidEntry = _startsWith(value, '/') || _startsWith(value, '://') || _startsWith(value, 'http://') || _startsWith(value, 'https://');
338
+ } else {
339
+ isValidEntry = String(value) === service.parseURL(value);
340
+ }
341
+ break;
342
+ case 'key':
343
+ isValidEntry = String(value) === service.machineName(value);
344
+ break;
345
+ case 'date':
346
+
347
+ //If we are being strict
348
+ if (strict) {
349
+ //And the input is not a javascript date object
350
+ if (typeof value != 'object' || !_isDate(value)) {
351
+ return false;
352
+ }
353
+ }
354
+
355
+ ///////////////////////////////
356
+ var parsed = service.parseDate(value);
357
+ return parsed !== undefined;
358
+ break;
359
+ case 'email':
360
+
361
+ if (strict) {
362
+ isValidEntry = value === service.parseEmail(value);
363
+ } else {
364
+ isValidEntry = String(value).toLowerCase() === service.parseEmail(value);
365
+ }
366
+
367
+ break;
368
+ case 'number':
369
+ case 'decimal':
370
+ case 'float':
371
+ if (strict) {
372
+ isValidEntry = valueIsNumber && (Number(value) === service.parseNumber(value));
373
+ } else {
374
+ isValidEntry = Number(value) === service.parseNumber(value);
375
+ }
376
+ break;
377
+ case 'integer':
378
+ if (strict) {
379
+ isValidEntry = valueIsNumber && (Number(value) === service.parseInt(value));
380
+ } else {
381
+ isValidEntry = Number(value) === service.parseInt(value);
382
+ }
383
+ break;
384
+ case 'boolean':
385
+
386
+ var parsed = service.parseBoolean(value);
387
+
388
+ //Only accept true booleans
389
+ if (strict) {
390
+ if (value === true || value === false) {
391
+ isValidEntry = value === parsed;
392
+ }
393
+ } else {
394
+ isValidEntry = parsed === true || parsed === false;
395
+ }
396
+ break;
397
+ case 'reference':
398
+ if (strict) {
399
+ isValidEntry = String(value) === service.id(value);
400
+ } else {
401
+ isValidEntry = !!service.id(value);
402
+ }
403
+ break;
404
+ case 'string':
405
+ var checkString = String(value);
406
+ if (strict) {
407
+ if (typeof value != 'string') {
408
+ return false;
409
+ }
410
+ }
411
+
412
+ if (checkString == '[object Object]') {
413
+ return false;
414
+ }
415
+
416
+ isValidEntry = true;
417
+ break;
418
+ // case 'group':
419
+ // isValidEntry = _isObject(value);
420
+ // break;
421
+ case 'object':
422
+ isValidEntry = _isObject(value) && !Array.isArray(value);
423
+ break;
424
+ case 'array':
425
+ isValidEntry = Array.isArray(value);
426
+ break;
427
+ }
428
+
429
+ ////////////////////////
430
+
431
+ return isValidEntry
432
+ }
433
+
434
+ service.parseBoolean = function(value) {
435
+ switch (String(value).toLowerCase()) {
436
+ case 'true':
437
+ case 'y':
438
+ case 'yes':
439
+ case '1':
440
+ case 't':
441
+ value = true;
442
+ break;
443
+ case 'false':
444
+ case 'n':
445
+ case 'no':
446
+ case '0':
447
+ case 'f':
448
+ case 'undefined':
449
+ case 'null':
450
+ case '':
451
+ case '-1':
452
+ value = false;
453
+ break;
454
+ }
455
+
456
+ return !!value;
457
+ }
458
+
459
+
460
+
461
+
462
+ ///////////////////////////////////////////////////////////////////////////////
463
+
464
+ service.clone = function(input) {
465
+ return JSON.parse(JSON.stringify(input));
466
+ }
467
+
468
+ ///////////////////////////////////////////////////////////////////////////////
469
+
470
+ service.mapFields = function(fields, options) {
471
+
472
+ if (!options) {
473
+ options = {};
474
+ }
475
+
476
+ //////////////////////////////
477
+
478
+ var output = [];
479
+ var trail = [];
480
+ var titles = [];
481
+ var currentDepth = 1;
482
+ var depthLimit = options.depth;
483
+
484
+
485
+ //Loop through each field
486
+ fields.forEach(mapField);
487
+
488
+ //////////////////////////////
489
+
490
+ //Recursively map the fields
491
+ function mapField(field, i) {
492
+
493
+
494
+ var fieldKey = field.key;
495
+ var isGroup = field.type == 'group';
496
+ var singleValue = (field.minimum === field.maximum) && field.minimum === 1;
497
+ var asObject = field.asObject; // || (isGroup && singleValue);
498
+ var isLayout = isGroup && !asObject;
499
+
500
+ //////////////////////////////////
501
+
502
+ var mapped = service.clone(field);
503
+
504
+
505
+ mapped.trail = trail.slice();
506
+ mapped.trail.push(fieldKey)
507
+ mapped.path = mapped.trail.join('.');
508
+
509
+
510
+ mapped.titles = titles.slice();
511
+ mapped.titles.push(field.title || fieldKey)
512
+
513
+
514
+ //If its an actual element or we've asked to include
515
+ //layout only elements
516
+ if (!isLayout || options.includeLayout) {
517
+ //Add it to the mix
518
+ output.push(mapped);
519
+ }
520
+
521
+ //////////////////////////////////
522
+
523
+ //Now see if there are child fields and should we go further
524
+ var limitHit = depthLimit && (currentDepth >= depthLimit);
525
+
526
+
527
+
528
+ //If there are child fields for this field
529
+ if (field.fields && field.fields.length) {
530
+ //If it's just a group with no extra key
531
+ if (isLayout) {
532
+ //Loop through fields as if they are at the same depth
533
+ field.fields.forEach(mapField)
534
+ } else {
535
+
536
+ //We don't need to traverse any further
537
+ //because we hit the limit
538
+ if (limitHit) {
539
+ return;
540
+ }
541
+
542
+ //Move down a level and add to the object
543
+ //Include the key in the path trail
544
+ currentDepth++;
545
+ trail.push(fieldKey);
546
+ titles.push(field.title || fieldKey);
547
+
548
+ //Loop through each field
549
+ field.fields.forEach(mapField)
550
+
551
+ //Move back up a level before we go to the next field
552
+ currentDepth--;
553
+ trail.pop();
554
+ titles.pop();
555
+ }
556
+
557
+ }
558
+
559
+ }
560
+
561
+ return output;
562
+ }
15
563
 
16
564
  ///////////////////////////////////////////////////////////////////////////////
17
565
 
@@ -24,27 +572,29 @@ var QikUtils = {};
24
572
  * //Returns &this=that&hello=world
25
573
  * qik.utils.mapParameters({"this":"that", "hello":"world"})
26
574
  */
27
- QikUtils.mapParameters = function(parameters) {
28
- return _.chain(parameters)
29
- .reduce(function(set, v, k) {
30
- if (v === undefined || v === null || v === false) {
31
- return set;
32
- }
575
+ service.mapParameters = function(parameters) {
33
576
 
34
- if (_.isArray(v)) {
35
- _.each(v, function(value) {
36
- set.push(`${k}=${encodeURIComponent(value)}`);
37
- })
577
+ var array = [];
38
578
 
39
- } else {
40
- set.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
41
- }
579
+ for (var key in parameters) {
580
+
581
+ var value = parameters[key];
582
+
583
+ if (value === undefined || value === null || value == false) {
584
+ return;
585
+ }
586
+
587
+ if (Array.isArray(value)) {
588
+ value.forEach(function(v) {
589
+ array.push(`${key}=${encodeURIComponent(v)}`);
590
+ })
591
+ } else {
592
+ array.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
593
+ }
594
+ }
595
+
596
+ return array.join('&');
42
597
 
43
- return set;
44
- }, [])
45
- .compact()
46
- .value()
47
- .join('&');
48
598
  }
49
599
 
50
600
  ///////////////////////////////////////////////////////////////////////////////
@@ -64,13 +614,13 @@ QikUtils.mapParameters = function(parameters) {
64
614
  * qik.utils.formatCurrency(1000, 'usd');
65
615
  *
66
616
  */
67
- QikUtils.formatCurrency = function(value, currency) {
617
+ service.formatCurrency = function(value, currency) {
68
618
 
69
619
  if (!value || isNaN(value)) {
70
620
  value = 0;
71
621
  }
72
622
 
73
- var currencyPrefix = QikUtils.currencySymbol(currency);
623
+ var currencyPrefix = service.currencySymbol(currency);
74
624
  return `${currencyPrefix}${parseFloat(parseInt(value) / 100).toFixed(2)}`;
75
625
 
76
626
  }
@@ -90,7 +640,7 @@ QikUtils.formatCurrency = function(value, currency) {
90
640
  * qik.utils.currencySymbol('usd');
91
641
  *
92
642
  */
93
- QikUtils.currencySymbol = function(currency) {
643
+ service.currencySymbol = function(currency) {
94
644
  //Ensure lowercase currency
95
645
  currency = String(currency).toLowerCase();
96
646
 
@@ -109,53 +659,53 @@ QikUtils.currencySymbol = function(currency) {
109
659
 
110
660
 
111
661
 
112
- QikUtils.getAvailableCurrencies = function(defaultCountryCode) {
662
+ service.getAvailableCurrencies = function(DefaultCountryID) {
113
663
 
114
664
 
115
665
  var array = [];
116
666
 
117
667
  array.push({
118
- name: `USD (${QikUtils.currencySymbol("usd")})`,
668
+ name: `USD (${service.currencySymbol("usd")})`,
119
669
  value: "usd",
120
670
  countryCode: { 'US': true },
121
671
  });
122
672
 
123
673
  array.push({
124
- name: `GBP (${QikUtils.currencySymbol("gbp")})`,
674
+ name: `GBP (${service.currencySymbol("gbp")})`,
125
675
  value: "gbp",
126
676
  countryCode: { 'GB': true, 'UK': true },
127
677
  });
128
678
 
129
679
  array.push({
130
- name: `CAD (${QikUtils.currencySymbol("cad")})`,
680
+ name: `CAD (${service.currencySymbol("cad")})`,
131
681
  value: "cad",
132
682
  countryCode: { 'CA': true },
133
683
  });
134
684
 
135
685
  array.push({
136
- name: `AUD (${QikUtils.currencySymbol("aud")})`,
686
+ name: `AUD (${service.currencySymbol("aud")})`,
137
687
  value: "aud",
138
688
  countryCode: { 'AU': true },
139
689
  });
140
690
 
141
691
 
142
692
  array.push({
143
- name: `NZD (${QikUtils.currencySymbol("nzd")})`,
693
+ name: `NZD (${service.currencySymbol("nzd")})`,
144
694
  value: "nzd",
145
695
  countryCode: { 'NZ': true },
146
696
  });
147
697
 
148
698
  array.push({
149
- name: `SGD (${QikUtils.currencySymbol("sgd")})`,
699
+ name: `SGD (${service.currencySymbol("sgd")})`,
150
700
  value: "sgd",
151
701
  countryCode: { 'SG': true },
152
702
  });
153
703
 
154
704
 
155
- if (defaultCountryCode) {
705
+ if (DefaultCountryID) {
156
706
 
157
707
  var findMatch = array.findIndex(function(currency) {
158
- return currency.countryCode[defaultCountryCode];
708
+ return currency.countryCode[DefaultCountryID];
159
709
  })
160
710
 
161
711
  const moveArrayItem = (array, fromIndex, toIndex) => {
@@ -185,19 +735,22 @@ QikUtils.getAvailableCurrencies = function(defaultCountryCode) {
185
735
  * qik.utils.mapReduce([{title:'test', definition:'something'}], 'definition');
186
736
  *
187
737
  */
188
- QikUtils.hash = function(array, key) {
738
+ service.hash = function(array, key) {
189
739
  if (!Array.isArray(array)) {
190
740
  array = [];
191
741
  }
192
742
  return array.reduce(function(set, item) {
193
- var key = _.get(item, key);
194
- set[key] = item;
743
+
744
+ var val = _get(item, key);
745
+
746
+ set[val] = item;
195
747
  return set;
196
748
  }, {});
197
749
  }
198
750
 
199
751
 
200
752
 
753
+
201
754
  ///////////////////////////////////////////////////////////////////////////////
202
755
 
203
756
  /**
@@ -208,7 +761,7 @@ QikUtils.hash = function(array, key) {
208
761
  * //Returns 20354d7a-e4fe-47af-8ff6-187bca92f3f9
209
762
  * qik.utils.guid()
210
763
  */
211
- QikUtils.guid = function() {
764
+ service.guid = function() {
212
765
  var u = (new Date()).getTime().toString(16) +
213
766
  Math.random().toString(16).substring(2) + "0".repeat(16);
214
767
  var guid = u.substr(0, 8) + '-' + u.substr(8, 4) + '-4000-8' +
@@ -239,7 +792,7 @@ QikUtils.guid = function() {
239
792
  * qik.utils.extractFromArray([{name:'Jerry', age:26}, {name:'Susan', age:19}], 'age', {sum:true});
240
793
  *
241
794
  */
242
- QikUtils.extractFromArray = function(array, key, sum, flatten, unique, exclude, options) {
795
+ service.extractFromArray = function(array, key, sum, flatten, unique, exclude, options) {
243
796
 
244
797
  if (!options) {
245
798
  options = {}
@@ -264,9 +817,9 @@ QikUtils.extractFromArray = function(array, key, sum, flatten, unique, exclude,
264
817
  /////////////////
265
818
 
266
819
  //Filter the array options by a certain value and operator
267
- var matches = _.reduce(array, function(set, entry) {
820
+ var matches = array.reduce(function(set, entry) {
268
821
  //Get the value from the object
269
- var retrievedValue = _.get(entry, key);
822
+ var retrievedValue = _get(entry, key);
270
823
 
271
824
  ///////////////////////
272
825
 
@@ -323,12 +876,12 @@ QikUtils.extractFromArray = function(array, key, sum, flatten, unique, exclude,
323
876
  * qik.utils.matchInArray([{name:'Jerry', age:26}, {name:'Susan', age:19}], 'age', 26, '>=');
324
877
  *
325
878
  */
326
- QikUtils.matchInArray = function(array, key, value, operator) {
879
+ service.matchInArray = function(array, key, value, operator) {
327
880
 
328
881
  //Filter the array options by a certain value and operator
329
- var matches = _.filter(array, function(entry) {
882
+ var matches = array.filter(function(entry) {
330
883
  //Get the value from the object
331
- var retrievedValue = _.get(entry, key);
884
+ var retrievedValue = _get(entry, key);
332
885
  var isMatch;
333
886
 
334
887
  ///////////////////////
@@ -348,7 +901,7 @@ QikUtils.matchInArray = function(array, key, value, operator) {
348
901
  isMatch = (retrievedValue <= value);
349
902
  break;
350
903
  case 'in':
351
- isMatch = _.includes(retrievedValue, value);
904
+ isMatch = retrievedValue.includes(value);
352
905
  break;
353
906
  default:
354
907
  //operator is strict equals
@@ -385,7 +938,7 @@ QikUtils.matchInArray = function(array, key, value, operator) {
385
938
  * //Returns 'cat, dog, bird'
386
939
  * qik.utils.comma([{title:'cat'}, {title:'dog'}, {title:'bird'}], 'title');
387
940
  */
388
- QikUtils.comma = function(array, path, limit) {
941
+ service.comma = function(array, path, limit) {
389
942
 
390
943
  if (limit) {
391
944
  array = array.slice(0, limit);
@@ -395,14 +948,13 @@ QikUtils.comma = function(array, path, limit) {
395
948
  .compact()
396
949
  .map(function(item) {
397
950
  if (path && path.length) {
398
- return _.get(item, path);
951
+ return _get(item, path);
399
952
  }
400
953
 
401
954
  return item;
402
955
  })
403
956
  .value()
404
957
  .join(', ');
405
-
406
958
  }
407
959
 
408
960
  ///////////////////////////////////////////////////////////////////////////////
@@ -422,12 +974,12 @@ QikUtils.comma = function(array, path, limit) {
422
974
  * qik.utils.id('5cb3d8b3a2219970e6f86927')
423
975
  *
424
976
  * //Returns true
425
- * typeof QikUtils.id({_id:'5cb3d8b3a2219970e6f86927', title, ...}) == 'string';
977
+ * typeof service.id({_id:'5cb3d8b3a2219970e6f86927', title, ...}) == 'string';
426
978
 
427
979
  * //Returns true
428
- * typeof QikUtils.id({_id:'5cb3d8b3a2219970e6f86927'}, true) == 'object';
980
+ * typeof service.id({_id:'5cb3d8b3a2219970e6f86927'}, true) == 'object';
429
981
  */
430
- QikUtils.id = function(source) {
982
+ service.id = function(source) {
431
983
 
432
984
  if (!source) {
433
985
  return;
@@ -465,29 +1017,39 @@ QikUtils.id = function(source) {
465
1017
  * //Returns ['5cb3d8b3a2219970e6f86927', '5cb3d8b3a2219970e6f86927', '5cb3d8b3a2219970e6f86927']
466
1018
  * qik.utils.ids([{_id:'5cb3d8b3a2219970e6f86927'}, {_id:'5cb3d8b3a2219970e6f86927'}, null, '5cb3d8b3a2219970e6f86927'])
467
1019
  */
468
- QikUtils.ids = function(array) {
469
- return _.chain(array)
470
- .reduce(function(set, entry) {
471
1020
 
472
- if (!entry) {
473
- return set;
474
- }
1021
+ service.ids = function(array) {
475
1022
 
476
- var cleaned = service.id(entry)
1023
+ if (!array) {
1024
+ return [];
1025
+ }
477
1026
 
478
- if (cleaned) {
479
- set[cleaned] = 1;
480
- }
1027
+ /////////////////////////////////
1028
+
1029
+ var ids = Object.keys(array.reduce(function(set, entry) {
481
1030
 
1031
+ if (!entry) {
482
1032
  return set;
483
- }, {})
484
- .keys()
485
- .value();
1033
+ }
1034
+
1035
+ var cleaned = service.id(entry)
1036
+
1037
+ if (cleaned) {
1038
+ set[cleaned] = 1;
1039
+ }
1040
+
1041
+ return set;
1042
+ }, {}))
1043
+
1044
+ ///////////////////////////////
1045
+
1046
+ return ids;
486
1047
  }
487
1048
 
1049
+
488
1050
  ///////////////////////////////////////////////////////////////////////////////
489
1051
 
490
- QikUtils.isValidID = function(input) {
1052
+ service.isValidID = function(input) {
491
1053
  var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$")
492
1054
  return checkForHexRegExp.test(String(input));
493
1055
 
@@ -501,32 +1063,38 @@ QikUtils.isValidID = function(input) {
501
1063
  * @param {Object} error The error object to translate
502
1064
  * @return {String} The resulting human readable error message
503
1065
  */
504
- QikUtils.errorMessage = function(err) {
1066
+ service.errorMessage = function(err) {
1067
+
1068
+ if (!err) {
1069
+ return;
1070
+ }
505
1071
 
506
1072
 
507
- if (_.isArray(err)) {
508
- err = _.first(err);
1073
+ if (Array.isArray(err)) {
1074
+ err = err[0];
509
1075
  }
510
1076
 
511
1077
  ////////////////////////////////////
512
1078
 
1079
+
513
1080
  var candidates = [
514
1081
  'response.data.message',
515
1082
  'response.data',
516
1083
  'message',
517
1084
  ]
518
1085
 
519
- ////////////////////////////////////
1086
+ var message;
520
1087
 
521
- var message = _.chain(candidates)
522
- .map(function(path) {
523
- return _.get(err, path);
524
- })
525
- .compact()
526
- .first()
527
- .value();
1088
+ candidates.forEach(function(path) {
1089
+ if (message) {
1090
+ return;
1091
+ }
528
1092
 
529
- ////////////////////////////////////
1093
+ var m = _get(err, path);
1094
+ if (m) {
1095
+ message = m;
1096
+ }
1097
+ })
530
1098
 
531
1099
  if (Array.isArray(message)) {
532
1100
  message = message[0];
@@ -534,9 +1102,7 @@ QikUtils.errorMessage = function(err) {
534
1102
 
535
1103
  ////////////////////////////////////
536
1104
 
537
- if (!message || !message.length) {
538
- return String(err);
539
- }
1105
+ message = message || JSON.stringify(err);
540
1106
 
541
1107
  ////////////////////////////////////
542
1108
 
@@ -544,10 +1110,6 @@ QikUtils.errorMessage = function(err) {
544
1110
  }
545
1111
 
546
1112
 
547
- /////////////////////////////////////////////
548
- /////////////////////////////////////////////
549
-
550
-
551
1113
 
552
1114
  ////////////////////////////////////
553
1115
  ////////////////////////////////////
@@ -560,22 +1122,25 @@ QikUtils.errorMessage = function(err) {
560
1122
  * @return {String} A cleaned and formatted string eg. (awesomeEvent)
561
1123
  */
562
1124
 
563
- QikUtils.machineName = function(string) {
1125
+ service.machineName = function(string) {
564
1126
 
565
1127
  if (!string || !string.length) {
566
1128
  return;
567
1129
  }
568
1130
 
1131
+
569
1132
  var regexp = /[^a-zA-Z0-9-_]+/g;
570
- return string.replace(regexp, '');
1133
+ var cleaned = String(string).replace(regexp, ' ');
1134
+ return _camelCase(cleaned);
571
1135
  }
572
1136
 
1137
+
573
1138
  /////////////////////////////////////////////
574
1139
  /////////////////////////////////////////////
575
1140
  /////////////////////////////////////////////
576
1141
 
577
1142
 
578
- export default QikUtils;
1143
+ export default service;
579
1144
 
580
1145
 
581
1146
  /////////////////////////////////////////////