@qikdev/sdk 0.1.16 → 0.1.20

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,562 @@
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
+ mapped.titles = titles.slice();
509
+ mapped.titles.push(field.title || fieldKey)
510
+
511
+ //If its an actual element or we've asked to include
512
+ //layout only elements
513
+ if (!isLayout || options.includeLayout) {
514
+ //Add it to the mix
515
+ output.push(mapped);
516
+ }
517
+
518
+ //////////////////////////////////
519
+
520
+ //Now see if there are child fields and should we go further
521
+ var limitHit = depthLimit && (currentDepth >= depthLimit);
522
+
523
+
524
+
525
+ //If there are child fields for this field
526
+ if (field.fields && field.fields.length) {
527
+ //If it's just a group with no extra key
528
+ if (isLayout) {
529
+ //Loop through fields as if they are at the same depth
530
+ field.fields.forEach(mapField)
531
+ } else {
532
+
533
+ //We don't need to traverse any further
534
+ //because we hit the limit
535
+ if (limitHit) {
536
+ return;
537
+ }
538
+
539
+ //Move down a level and add to the object
540
+ //Include the key in the path trail
541
+ currentDepth++;
542
+ trail.push(fieldKey);
543
+ titles.push(field.title || fieldKey);
544
+
545
+ //Loop through each field
546
+ field.fields.forEach(mapField)
547
+
548
+ //Move back up a level before we go to the next field
549
+ currentDepth--;
550
+ trail.pop();
551
+ titles.pop();
552
+ }
553
+
554
+ }
555
+
556
+ }
557
+
558
+ return output;
559
+ }
15
560
 
16
561
  ///////////////////////////////////////////////////////////////////////////////
17
562
 
@@ -24,27 +569,29 @@ var QikUtils = {};
24
569
  * //Returns &this=that&hello=world
25
570
  * qik.utils.mapParameters({"this":"that", "hello":"world"})
26
571
  */
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
- }
572
+ service.mapParameters = function(parameters) {
33
573
 
34
- if (_.isArray(v)) {
35
- _.each(v, function(value) {
36
- set.push(`${k}=${encodeURIComponent(value)}`);
37
- })
574
+ var array = [];
38
575
 
39
- } else {
40
- set.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
41
- }
576
+ for (var key in parameters) {
577
+
578
+ var value = parameters[key];
579
+
580
+ if (value === undefined || value === null || value == false) {
581
+ return;
582
+ }
583
+
584
+ if (Array.isArray(value)) {
585
+ value.forEach(function(v) {
586
+ array.push(`${key}=${encodeURIComponent(v)}`);
587
+ })
588
+ } else {
589
+ array.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
590
+ }
591
+ }
592
+
593
+ return array.join('&');
42
594
 
43
- return set;
44
- }, [])
45
- .compact()
46
- .value()
47
- .join('&');
48
595
  }
49
596
 
50
597
  ///////////////////////////////////////////////////////////////////////////////
@@ -64,13 +611,13 @@ QikUtils.mapParameters = function(parameters) {
64
611
  * qik.utils.formatCurrency(1000, 'usd');
65
612
  *
66
613
  */
67
- QikUtils.formatCurrency = function(value, currency) {
614
+ service.formatCurrency = function(value, currency) {
68
615
 
69
616
  if (!value || isNaN(value)) {
70
617
  value = 0;
71
618
  }
72
619
 
73
- var currencyPrefix = QikUtils.currencySymbol(currency);
620
+ var currencyPrefix = service.currencySymbol(currency);
74
621
  return `${currencyPrefix}${parseFloat(parseInt(value) / 100).toFixed(2)}`;
75
622
 
76
623
  }
@@ -90,7 +637,7 @@ QikUtils.formatCurrency = function(value, currency) {
90
637
  * qik.utils.currencySymbol('usd');
91
638
  *
92
639
  */
93
- QikUtils.currencySymbol = function(currency) {
640
+ service.currencySymbol = function(currency) {
94
641
  //Ensure lowercase currency
95
642
  currency = String(currency).toLowerCase();
96
643
 
@@ -109,53 +656,53 @@ QikUtils.currencySymbol = function(currency) {
109
656
 
110
657
 
111
658
 
112
- QikUtils.getAvailableCurrencies = function(defaultCountryCode) {
659
+ service.getAvailableCurrencies = function(DefaultCountryID) {
113
660
 
114
661
 
115
662
  var array = [];
116
663
 
117
664
  array.push({
118
- name: `USD (${QikUtils.currencySymbol("usd")})`,
665
+ name: `USD (${service.currencySymbol("usd")})`,
119
666
  value: "usd",
120
667
  countryCode: { 'US': true },
121
668
  });
122
669
 
123
670
  array.push({
124
- name: `GBP (${QikUtils.currencySymbol("gbp")})`,
671
+ name: `GBP (${service.currencySymbol("gbp")})`,
125
672
  value: "gbp",
126
673
  countryCode: { 'GB': true, 'UK': true },
127
674
  });
128
675
 
129
676
  array.push({
130
- name: `CAD (${QikUtils.currencySymbol("cad")})`,
677
+ name: `CAD (${service.currencySymbol("cad")})`,
131
678
  value: "cad",
132
679
  countryCode: { 'CA': true },
133
680
  });
134
681
 
135
682
  array.push({
136
- name: `AUD (${QikUtils.currencySymbol("aud")})`,
683
+ name: `AUD (${service.currencySymbol("aud")})`,
137
684
  value: "aud",
138
685
  countryCode: { 'AU': true },
139
686
  });
140
687
 
141
688
 
142
689
  array.push({
143
- name: `NZD (${QikUtils.currencySymbol("nzd")})`,
690
+ name: `NZD (${service.currencySymbol("nzd")})`,
144
691
  value: "nzd",
145
692
  countryCode: { 'NZ': true },
146
693
  });
147
694
 
148
695
  array.push({
149
- name: `SGD (${QikUtils.currencySymbol("sgd")})`,
696
+ name: `SGD (${service.currencySymbol("sgd")})`,
150
697
  value: "sgd",
151
698
  countryCode: { 'SG': true },
152
699
  });
153
700
 
154
701
 
155
- if (defaultCountryCode) {
702
+ if (DefaultCountryID) {
156
703
 
157
704
  var findMatch = array.findIndex(function(currency) {
158
- return currency.countryCode[defaultCountryCode];
705
+ return currency.countryCode[DefaultCountryID];
159
706
  })
160
707
 
161
708
  const moveArrayItem = (array, fromIndex, toIndex) => {
@@ -185,19 +732,22 @@ QikUtils.getAvailableCurrencies = function(defaultCountryCode) {
185
732
  * qik.utils.mapReduce([{title:'test', definition:'something'}], 'definition');
186
733
  *
187
734
  */
188
- QikUtils.hash = function(array, key) {
735
+ service.hash = function(array, key) {
189
736
  if (!Array.isArray(array)) {
190
737
  array = [];
191
738
  }
192
739
  return array.reduce(function(set, item) {
193
- var key = _.get(item, key);
194
- set[key] = item;
740
+
741
+ var val = _get(item, key);
742
+
743
+ set[val] = item;
195
744
  return set;
196
745
  }, {});
197
746
  }
198
747
 
199
748
 
200
749
 
750
+
201
751
  ///////////////////////////////////////////////////////////////////////////////
202
752
 
203
753
  /**
@@ -208,7 +758,7 @@ QikUtils.hash = function(array, key) {
208
758
  * //Returns 20354d7a-e4fe-47af-8ff6-187bca92f3f9
209
759
  * qik.utils.guid()
210
760
  */
211
- QikUtils.guid = function() {
761
+ service.guid = function() {
212
762
  var u = (new Date()).getTime().toString(16) +
213
763
  Math.random().toString(16).substring(2) + "0".repeat(16);
214
764
  var guid = u.substr(0, 8) + '-' + u.substr(8, 4) + '-4000-8' +
@@ -239,7 +789,7 @@ QikUtils.guid = function() {
239
789
  * qik.utils.extractFromArray([{name:'Jerry', age:26}, {name:'Susan', age:19}], 'age', {sum:true});
240
790
  *
241
791
  */
242
- QikUtils.extractFromArray = function(array, key, sum, flatten, unique, exclude, options) {
792
+ service.extractFromArray = function(array, key, sum, flatten, unique, exclude, options) {
243
793
 
244
794
  if (!options) {
245
795
  options = {}
@@ -264,9 +814,9 @@ QikUtils.extractFromArray = function(array, key, sum, flatten, unique, exclude,
264
814
  /////////////////
265
815
 
266
816
  //Filter the array options by a certain value and operator
267
- var matches = _.reduce(array, function(set, entry) {
817
+ var matches = array.reduce(function(set, entry) {
268
818
  //Get the value from the object
269
- var retrievedValue = _.get(entry, key);
819
+ var retrievedValue = _get(entry, key);
270
820
 
271
821
  ///////////////////////
272
822
 
@@ -323,12 +873,12 @@ QikUtils.extractFromArray = function(array, key, sum, flatten, unique, exclude,
323
873
  * qik.utils.matchInArray([{name:'Jerry', age:26}, {name:'Susan', age:19}], 'age', 26, '>=');
324
874
  *
325
875
  */
326
- QikUtils.matchInArray = function(array, key, value, operator) {
876
+ service.matchInArray = function(array, key, value, operator) {
327
877
 
328
878
  //Filter the array options by a certain value and operator
329
- var matches = _.filter(array, function(entry) {
879
+ var matches = array.filter(function(entry) {
330
880
  //Get the value from the object
331
- var retrievedValue = _.get(entry, key);
881
+ var retrievedValue = _get(entry, key);
332
882
  var isMatch;
333
883
 
334
884
  ///////////////////////
@@ -348,7 +898,7 @@ QikUtils.matchInArray = function(array, key, value, operator) {
348
898
  isMatch = (retrievedValue <= value);
349
899
  break;
350
900
  case 'in':
351
- isMatch = _.includes(retrievedValue, value);
901
+ isMatch = retrievedValue.includes(value);
352
902
  break;
353
903
  default:
354
904
  //operator is strict equals
@@ -385,7 +935,7 @@ QikUtils.matchInArray = function(array, key, value, operator) {
385
935
  * //Returns 'cat, dog, bird'
386
936
  * qik.utils.comma([{title:'cat'}, {title:'dog'}, {title:'bird'}], 'title');
387
937
  */
388
- QikUtils.comma = function(array, path, limit) {
938
+ service.comma = function(array, path, limit) {
389
939
 
390
940
  if (limit) {
391
941
  array = array.slice(0, limit);
@@ -395,14 +945,13 @@ QikUtils.comma = function(array, path, limit) {
395
945
  .compact()
396
946
  .map(function(item) {
397
947
  if (path && path.length) {
398
- return _.get(item, path);
948
+ return _get(item, path);
399
949
  }
400
950
 
401
951
  return item;
402
952
  })
403
953
  .value()
404
954
  .join(', ');
405
-
406
955
  }
407
956
 
408
957
  ///////////////////////////////////////////////////////////////////////////////
@@ -422,12 +971,12 @@ QikUtils.comma = function(array, path, limit) {
422
971
  * qik.utils.id('5cb3d8b3a2219970e6f86927')
423
972
  *
424
973
  * //Returns true
425
- * typeof QikUtils.id({_id:'5cb3d8b3a2219970e6f86927', title, ...}) == 'string';
974
+ * typeof service.id({_id:'5cb3d8b3a2219970e6f86927', title, ...}) == 'string';
426
975
 
427
976
  * //Returns true
428
- * typeof QikUtils.id({_id:'5cb3d8b3a2219970e6f86927'}, true) == 'object';
977
+ * typeof service.id({_id:'5cb3d8b3a2219970e6f86927'}, true) == 'object';
429
978
  */
430
- QikUtils.id = function(source) {
979
+ service.id = function(source) {
431
980
 
432
981
  if (!source) {
433
982
  return;
@@ -465,29 +1014,39 @@ QikUtils.id = function(source) {
465
1014
  * //Returns ['5cb3d8b3a2219970e6f86927', '5cb3d8b3a2219970e6f86927', '5cb3d8b3a2219970e6f86927']
466
1015
  * qik.utils.ids([{_id:'5cb3d8b3a2219970e6f86927'}, {_id:'5cb3d8b3a2219970e6f86927'}, null, '5cb3d8b3a2219970e6f86927'])
467
1016
  */
468
- QikUtils.ids = function(array) {
469
- return _.chain(array)
470
- .reduce(function(set, entry) {
471
1017
 
472
- if (!entry) {
473
- return set;
474
- }
1018
+ service.ids = function(array) {
475
1019
 
476
- var cleaned = service.id(entry)
1020
+ if (!array) {
1021
+ return [];
1022
+ }
477
1023
 
478
- if (cleaned) {
479
- set[cleaned] = 1;
480
- }
1024
+ /////////////////////////////////
1025
+
1026
+ var ids = Object.keys(array.reduce(function(set, entry) {
481
1027
 
1028
+ if (!entry) {
482
1029
  return set;
483
- }, {})
484
- .keys()
485
- .value();
1030
+ }
1031
+
1032
+ var cleaned = service.id(entry)
1033
+
1034
+ if (cleaned) {
1035
+ set[cleaned] = 1;
1036
+ }
1037
+
1038
+ return set;
1039
+ }, {}))
1040
+
1041
+ ///////////////////////////////
1042
+
1043
+ return ids;
486
1044
  }
487
1045
 
1046
+
488
1047
  ///////////////////////////////////////////////////////////////////////////////
489
1048
 
490
- QikUtils.isValidID = function(input) {
1049
+ service.isValidID = function(input) {
491
1050
  var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$")
492
1051
  return checkForHexRegExp.test(String(input));
493
1052
 
@@ -501,32 +1060,38 @@ QikUtils.isValidID = function(input) {
501
1060
  * @param {Object} error The error object to translate
502
1061
  * @return {String} The resulting human readable error message
503
1062
  */
504
- QikUtils.errorMessage = function(err) {
1063
+ service.errorMessage = function(err) {
1064
+
1065
+ if (!err) {
1066
+ return;
1067
+ }
505
1068
 
506
1069
 
507
- if (_.isArray(err)) {
508
- err = _.first(err);
1070
+ if (Array.isArray(err)) {
1071
+ err = err[0];
509
1072
  }
510
1073
 
511
1074
  ////////////////////////////////////
512
1075
 
1076
+
513
1077
  var candidates = [
514
1078
  'response.data.message',
515
1079
  'response.data',
516
1080
  'message',
517
1081
  ]
518
1082
 
519
- ////////////////////////////////////
1083
+ var message;
520
1084
 
521
- var message = _.chain(candidates)
522
- .map(function(path) {
523
- return _.get(err, path);
524
- })
525
- .compact()
526
- .first()
527
- .value();
1085
+ candidates.forEach(function(path) {
1086
+ if (message) {
1087
+ return;
1088
+ }
528
1089
 
529
- ////////////////////////////////////
1090
+ var m = _get(err, path);
1091
+ if (m) {
1092
+ message = m;
1093
+ }
1094
+ })
530
1095
 
531
1096
  if (Array.isArray(message)) {
532
1097
  message = message[0];
@@ -534,9 +1099,7 @@ QikUtils.errorMessage = function(err) {
534
1099
 
535
1100
  ////////////////////////////////////
536
1101
 
537
- if (!message || !message.length) {
538
- return String(err);
539
- }
1102
+ message = message || JSON.stringify(err);
540
1103
 
541
1104
  ////////////////////////////////////
542
1105
 
@@ -544,10 +1107,6 @@ QikUtils.errorMessage = function(err) {
544
1107
  }
545
1108
 
546
1109
 
547
- /////////////////////////////////////////////
548
- /////////////////////////////////////////////
549
-
550
-
551
1110
 
552
1111
  ////////////////////////////////////
553
1112
  ////////////////////////////////////
@@ -560,22 +1119,25 @@ QikUtils.errorMessage = function(err) {
560
1119
  * @return {String} A cleaned and formatted string eg. (awesomeEvent)
561
1120
  */
562
1121
 
563
- QikUtils.machineName = function(string) {
1122
+ service.machineName = function(string) {
564
1123
 
565
1124
  if (!string || !string.length) {
566
1125
  return;
567
1126
  }
568
1127
 
1128
+
569
1129
  var regexp = /[^a-zA-Z0-9-_]+/g;
570
- return string.replace(regexp, '');
1130
+ var cleaned = String(string).replace(regexp, ' ');
1131
+ return _camelCase(cleaned);
571
1132
  }
572
1133
 
1134
+
573
1135
  /////////////////////////////////////////////
574
1136
  /////////////////////////////////////////////
575
1137
  /////////////////////////////////////////////
576
1138
 
577
1139
 
578
- export default QikUtils;
1140
+ export default service;
579
1141
 
580
1142
 
581
1143
  /////////////////////////////////////////////