-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.html
4274 lines (4197 loc) · 118 KB
/
README.html
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html>
<head>
<title>BAMF JS</title>
<style>
html, body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
}
body,
input,
textarea,
select {
font-size: 100%;
}
</style>
</head>
<body>
<!-- markdownlint-disable MD036 -->
<h1>ksjs</h1>
<p>This repo contains a bunch of plain JavaScript functions that come in handy while working on various projects. They are mostly provided as ES modules, but a subset of them are also offered as CommonJS modules so they can easily be used in an older node.js environment.</p>
<h2>Install</h2>
<p>From the command line, run:</p>
<pre><code class="language-bash">npm install ksjs
</code></pre>
<p>or</p>
<pre><code class="language-bash">yarn add ksjs
</code></pre>
<h2>ES Modules</h2>
<p><strong>Preferred</strong>: For any of the <a href="#modules">modules</a>, you can import functions like so:</p>
<pre><code class="language-js">import {example1, example2} from 'ksjs/example.mjs'
// Depending on your project, ES modules are available in
// files with the .js extension, too. For example:
// import {example1, example2} from 'ksjs/example.js'
example1('foo');
example2('bar');
</code></pre>
<p><strong>Not recommended</strong>: If your bundler supports ES module tree shaking, you might be able to import functions from various files like so (for Webpack, you might need to configure it to treat bamfjs as ES6+):</p>
<pre><code class="language-js">import {$, debounce, deepCopy} from 'bamfjs';
</code></pre>
<h2>CommonJS Modules</h2>
<p>The following <a href="#modules">modules</a> & their corresponding functions can be used in a node.js environment:</p>
<ul>
<li>array</li>
<li>color</li>
<li>math</li>
<li>object</li>
<li>promise</li>
<li>string</li>
<li>timer</li>
<li>url</li>
</ul>
<p><strong>Preferred</strong>: You can require them from their respective files with the <code>.cjs</code> extension, like so:</p>
<pre><code class="language-js">const {example1} = require('ksjs/example.cjs');
example1('foo');
</code></pre>
<p>or like so:</p>
<pre><code class="language-js">const examples = require('ksjs/example.cjs');
examples.example1('foo');
</code></pre>
<p><strong>Otherwise</strong>: You could require them from the <code>cjs</code> directory, like so (Note the “.js” extension here):</p>
<pre><code class="language-js">const {example1} = require('ksjs/cjs/example.js');
example1('foo');
</code></pre>
<p>or like so:</p>
<pre><code class="language-js">const examples = require('ksjs/cjs/example.js');
examples.example1('foo');
</code></pre>
<p><a id="modules"></a></p>
<hr>
<h2>Modules</h2>
<ul>
<li><a href="#module_ajax">ajax</a></li>
<li><a href="#module_array">array</a></li>
<li><a href="#module_color">color</a></li>
<li><a href="#module_cookie">cookie</a></li>
<li><a href="#module_dom">dom</a></li>
<li><a href="#module_event">event</a></li>
<li><a href="#module_form">form</a></li>
<li><a href="#module_jsonp">jsonp</a></li>
<li><a href="#module_math">math</a></li>
<li><a href="#module_object">object</a></li>
<li><a href="#module_promise">promise</a></li>
<li><a href="#module_selection">selection</a></li>
<li><a href="#module_storage">storage</a></li>
<li><a href="#module_string">string</a></li>
<li><a href="#module_timer">timer</a></li>
<li><a href="#module_url">url</a>
<a name="module_ajax"></a></li>
</ul>
<h2>ajax</h2>
<p>ESM Import Example:</p>
<pre><code class="language-js">import {getJSON} from 'ksjs';
// or:
import {getJSON} from 'ksjs/ajax.js';
// or:
import {getJSON} from 'ksjs/ajax.mjs';
</code></pre>
<ul>
<li><a href="#module_ajax">ajax</a>
<ul>
<li><a href="#module_ajax..ajax">ajax([url], options)</a> ⇒ <code>Promise</code></li>
<li><a href="#module_ajax..getJSON">getJSON([url], [options])</a> ⇒ <code>Promise</code></li>
<li><a href="#module_ajax..postJSON">postJSON([url], [options])</a> ⇒ <code>Promise</code></li>
<li><a href="#module_ajax..postFormData">postFormData([url], [options])</a> ⇒ <code>Promise</code></li>
<li><a href="#module_ajax..fetchHTML">fetchHTML(url, selector)</a> ⇒ <code>Promise</code></li>
</ul>
</li>
</ul>
<p><a name="module_ajax..ajax"></a></p>
<h3>ajax([url], options) ⇒ <code>Promise</code></h3>
<p>Low-level ajax request</p>
<p><strong>Returns</strong>: <code>Promise</code> - A resolved or rejected Promise from the server<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>[url]</td>
<td><code>string</code></td>
<td><code>location.href</code></td>
<td>The URL of the resource</td>
</tr>
<tr>
<td>options</td>
<td><code>object</code></td>
<td></td>
<td></td>
</tr>
<tr>
<td>[options.dataType]</td>
<td><code>string</code></td>
<td></td>
<td>One of ‘json’, ‘html’, ‘xml’, ‘form’, ‘formData’. Used for setting the <code>Content-Type</code> request header (e.g. <code>multipart/form-data</code> when 'formData`) and processing the response (e.g. calling JSON.parse() on a string response when ‘json’);</td>
</tr>
<tr>
<td>[options.data]</td>
<td><code>Object</code> | <code>string</code></td>
<td></td>
<td>Data to send along with the request. If it’s a GET request and <code>options.data</code> is an object, the object is converted to a query string and appended to the URL.</td>
</tr>
<tr>
<td>[options.method]</td>
<td><code>string</code></td>
<td><code>"GET"</code></td>
<td>One of ‘GET’, ‘POST’, etc.</td>
</tr>
<tr>
<td>[options.cache]</td>
<td><code>boolean</code></td>
<td><code>true</code></td>
<td>If set to <code>false</code>, will not let server use cached response</td>
</tr>
<tr>
<td>[options.memcache]</td>
<td><code>boolean</code></td>
<td><code>false</code></td>
<td>If set to <code>true</code>, and a previous request sent to the same url was successful, will circumvent request and use the previous response</td>
</tr>
<tr>
<td>[options.headers]</td>
<td><code>Object</code></td>
<td><code>{}</code></td>
<td><strong>Advanced</strong>: Additional headers to send with the request. If headers such as ‘Accept’, ‘Content-Type’, ‘Cache-Control’, ‘X-Requested-With’, etc., are set here, they will override their respective headers set automatically based on other options such as <code>options.dataType</code> and <code>options.cache</code>.</td>
</tr>
<tr>
<td>[options.form]</td>
<td><code>HTMLFormElement</code></td>
<td></td>
<td>An optional form element</td>
</tr>
</tbody>
</table>
<p><a name="module_ajax..getJSON"></a></p>
<h3>getJSON([url], [options]) ⇒ <code>Promise</code></h3>
<p>Send a GET request and return parsed JSON response from the resolved Promise</p>
<p><strong>Returns</strong>: <code>Promise</code> - A resolved or rejected Promise from the server<br /></p>
<p><strong>See</strong>: <a href="#module_ajax..ajax">ajax</a></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>[url]</td>
<td><code>string</code></td>
<td><code>location.href</code></td>
<td>The URL of the resource</td>
</tr>
<tr>
<td>[options]</td>
<td><code>Object</code></td>
<td><code>{}</code></td>
<td>See <a href="#module_ajax..ajax">ajax</a> for details</td>
</tr>
</tbody>
</table>
<p><a name="module_ajax..postJSON"></a></p>
<h3>postJSON([url], [options]) ⇒ <code>Promise</code></h3>
<p>Send a POST request and return parsed JSON response from the resolved Promise</p>
<p><strong>Returns</strong>: <code>Promise</code> - A resolved or rejected Promise from the server<br /></p>
<p><strong>See</strong>: <a href="#module_ajax..ajax">ajax</a></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>[url]</td>
<td><code>string</code></td>
<td><code>location.href</code></td>
<td>The URL of the resource</td>
</tr>
<tr>
<td>[options]</td>
<td><code>Object</code></td>
<td><code>{}</code></td>
<td>See <a href="#module_ajax..ajax">ajax</a> for details</td>
</tr>
</tbody>
</table>
<p><a name="module_ajax..postFormData"></a></p>
<h3>postFormData([url], [options]) ⇒ <code>Promise</code></h3>
<p>Send a POST request with <code>FormData</code> derived from form element provided by <code>options.form</code></p>
<p><strong>Returns</strong>: <code>Promise</code> - A resolved or rejected Promise from the server<br /></p>
<p><strong>See</strong>: <a href="#module_ajax..ajax">ajax</a></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>[url]</td>
<td><code>string</code></td>
<td><code>location.href</code></td>
<td>The URL of the resource</td>
</tr>
<tr>
<td>[options]</td>
<td><code>Object</code></td>
<td><code>{}</code></td>
<td>See <a href="#module_ajax..ajax">ajax</a> for details</td>
</tr>
</tbody>
</table>
<p><a name="module_ajax..fetchHTML"></a></p>
<h3>fetchHTML(url, selector) ⇒ <code>Promise</code></h3>
<p>Fetch an HTML document and return the html string (or a subset of it) from the resolved Promise</p>
<p><strong>Returns</strong>: <code>Promise</code> - A resolved or rejected Promise, resolving to an HTML string<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>url</td>
<td><code>string</code></td>
<td>The URL of the resource to fetch</td>
</tr>
<tr>
<td>selector</td>
<td><code>string</code></td>
<td>A selector specifying the html content in the resource to return</td>
</tr>
</tbody>
</table>
<p><a name="module_array"></a></p>
<h2>array</h2>
<p>ESM Import Example:</p>
<pre><code class="language-js">import {isArray} from 'ksjs';
// or:
import {isArray} from 'ksjs/array.mjs';
// or:
import {isArray} from 'ksjs/array.js';
</code></pre>
<p>CommonJS Require Example:</p>
<pre><code class="language-js">const {isArray} = require('ksjs/array.cjs');
// or:
const {isArray} = require('ksjs/cjs/array.js');
</code></pre>
<ul>
<li><a href="#module_array">array</a>
<ul>
<li><a href="#module_array..isArray">isArray(arr)</a> ⇒ <code>boolean</code></li>
<li><a href="#module_array..inArray">inArray(el, arr)</a> ⇒ <code>boolean</code></li>
<li><a href="#module_array..objectToArray">objectToArray(obj)</a> ⇒ <code>array</code></li>
<li><a href="#module_array..makeArray">makeArray(value, [delimiter], [wrapObject])</a> ⇒ <code>array</code></li>
<li><a href="#module_array..randomItem">randomItem(arr)</a> ⇒ <code>any</code></li>
<li><a href="#module_array..pluck">pluck(arr, prop)</a> ⇒ <code>array</code></li>
<li><a href="#module_array..shuffle">shuffle(els)</a> ⇒ <code>array</code></li>
<li><a href="#module_array..merge">merge(…arrays)</a> ⇒ <code>array</code></li>
<li><s><a href="#module_array..collapse">collapse()</a></s></li>
<li><a href="#module_array..intersect">intersect(array1, array2, [prop])</a> ⇒ <code>array</code></li>
<li><a href="#module_array..unique">unique(arr, [prop])</a> ⇒ <code>array</code></li>
<li><a href="#module_array..diff">diff(array1, array2, [prop])</a> ⇒ <code>array</code></li>
<li><a href="#module_array..chunk">chunk(arr, n)</a> ⇒ <code>array</code></li>
<li><a href="#module_array..range">range(a, [b])</a> ⇒ <code>array</code></li>
<li><a href="#module_array..pad">pad(arr, size, value)</a> ⇒ <code>array</code></li>
<li><a href="#module_array..sort">sort(arr, [prop], [options])</a> ⇒ <code>array</code></li>
</ul>
</li>
</ul>
<p><a name="module_array..isArray"></a></p>
<h3>isArray(arr) ⇒ <code>boolean</code></h3>
<p>Determine whether “arr” is a true array</p>
<p><strong>Returns</strong>: <code>boolean</code> - <code>true</code> if arr is array, <code>false</code> if not<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>arr</td>
<td><code>array</code></td>
<td>item to determine whether it’s an array</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">import {isArray} from 'ksjs/array.js';
if (isArray(window.foo)) {
window.foo.push('bar');
}
</code></pre>
<p><a name="module_array..inArray"></a></p>
<h3>inArray(el, arr) ⇒ <code>boolean</code></h3>
<p>Determine whether item “el” is in array “arr”</p>
<p><strong>Returns</strong>: <code>boolean</code> - Boolean (<code>true</code> if el is in array, <code>false</code> if not)<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>el</td>
<td><code>any</code></td>
<td>An item to test against the array</td>
</tr>
<tr>
<td>arr</td>
<td><code>array</code></td>
<td>The array to test against</td>
</tr>
</tbody>
</table>
<p><a name="module_array..objectToArray"></a></p>
<h3>objectToArray(obj) ⇒ <code>array</code></h3>
<p>Convert an object to an array of objects with name and value properties</p>
<p><strong>Returns</strong>: <code>array</code> - An array of objects with name and value properties<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>obj</td>
<td><code>object</code></td>
<td>The object to convert</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">import {objectToArray} from 'ksjs/array.js';
const obj = {
foo: 'bar',
baz: 'qux'
};
const arr = objectToArray(obj);
// arr = [
// {name: 'foo', value: 'bar'},
// {name: 'baz', value: 'qux'}
// ];
</code></pre>
<p><a name="module_array..makeArray"></a></p>
<h3>makeArray(value, [delimiter], [wrapObject]) ⇒ <code>array</code></h3>
<p>Return an array based on the given value:
a) Strings are split by a delimiter (defaults to /\s+/).
b) Plain objects are converted to an array of objects with name and value properties.
b2) …unless wrapObject is true in which case they are just wrapped in an array
c) Undefined and null are returned as an empty array.
d) Arrays are returned as is.
e) Anything else is wrapped in an array.</p>
<p><strong>Returns</strong>: <code>array</code> - The value converted to an array<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>value</td>
<td><code>any</code></td>
<td></td>
<td>The value to convert to an array</td>
</tr>
<tr>
<td>[delimiter]</td>
<td><code>string</code> | <code>RegExp</code></td>
<td><code>"= /\s+/"</code></td>
<td>A string or regular expression to use for splitting a string into an array (defaults to /\s+/)</td>
</tr>
<tr>
<td>[wrapObject]</td>
<td><code>Boolean</code></td>
<td></td>
<td>Whether to simply wrap an object in an array (true) or convert to array of objects with name/value properties</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">import {makeArray} from 'ksjs/array.js';
const foo = makeArray('one two three');
// foo is now ['one', 'two', 'three']
const bar = makeArray('one,two,three', ',');
// bar is now ['one', 'two', 'three']
const baz = makeArray(['one', 'two', 'three']);
// baz is still ['one', 'two', 'three']
const quz = makeArray({foo: 'bar'});
// quz is now [{name: 'foo': value: 'bar'}]
const quuz = makeArray(null);
// quuz is now []
</code></pre>
<p><a name="module_array..randomItem"></a></p>
<h3>randomItem(arr) ⇒ <code>any</code></h3>
<p>Return a random item from the provided array</p>
<p><strong>Returns</strong>: <code>any</code> - A random element from the provided array<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>arr</td>
<td><code>array</code></td>
<td>An array of elements</td>
</tr>
</tbody>
</table>
<p><a name="module_array..pluck"></a></p>
<h3>pluck(arr, prop) ⇒ <code>array</code></h3>
<p>Take an array of objects and a property and return an array of values of that property</p>
<p><strong>Returns</strong>: <code>array</code> - Array of values of the property (if the value is <code>undefined</code>, returns <code>null</code> instead)<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>arr</td>
<td><code>array</code></td>
<td>Array from which to pluck</td>
</tr>
<tr>
<td>prop</td>
<td><code>string</code></td>
<td>Property to pluck</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">import {pluck} from 'ksjs/array.js';
let family = [
{
id: 'dad',
name: 'Karl'
},
{
id: 'mom',
name: 'Sara',
color: 'blue'
},
{
id: 'son',
name: 'Ben',
color: 'green'
},
{
id: 'daughter',
name: 'Lucy'
}
];
let names = pluck(family, 'name');
let ids = pluck(family, 'id');
let colors = pluck(family, 'color');
console.log(names);
// Logs: ['Karl', 'Sara', 'Ben', 'Lucy']
console.log(ids);
// Logs: ['dad', 'mom', 'son', 'daughter']
console.log(colors);
// Logs: [null, 'blue', 'green', null]
</code></pre>
<p><a name="module_array..shuffle"></a></p>
<h3>shuffle(els) ⇒ <code>array</code></h3>
<p>Fisher-Yates (aka Knuth) shuffle. Takes an array of elements and returns the same array, but with its elements shuffled</p>
<p><strong>Returns</strong>: <code>array</code> - The array passed to <code>arr</code>, shuffled<br /></p>
<p><strong>See</strong>: <a href="https://github.com/coolaj86/knuth-shuffle">knuth-shuffle</a></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>els</td>
<td><code>array</code></td>
<td>Array to be shuffled</td>
</tr>
</tbody>
</table>
<p><a name="module_array..merge"></a></p>
<h3>merge(…arrays) ⇒ <code>array</code></h3>
<p>Merge two or more arrays into a single, new array.</p>
<p><strong>Returns</strong>: <code>array</code> - A new merged array<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>…arrays</td>
<td><code>array</code></td>
<td>2 or more arrays to collapse</td>
</tr>
</tbody>
</table>
<p><a name="module_array..collapse"></a></p>
<h3><s>collapse()</s></h3>
<p><em><strong>Deprecated</strong></em></p>
<p><strong>See</strong>: <a href="#module_array..merge">merge</a> instead
<a name="module_array..intersect"></a></p>
<h3>intersect(array1, array2, [prop]) ⇒ <code>array</code></h3>
<p>Return a subset of <code>array1</code>, only including elements from <code>array2</code> that are also in <code>array1</code>.</p>
<ul>
<li>If <code>prop</code> is provided, only that property of an element needs to match for the two arrays to be considered intersecting at that element</li>
</ul>
<p><strong>Returns</strong>: <code>array</code> - A new filtered array<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>array1</td>
<td><code>array</code></td>
<td>First array</td>
</tr>
<tr>
<td>array2</td>
<td><code>array</code></td>
<td>Second array</td>
</tr>
<tr>
<td>[prop]</td>
<td><code>any</code></td>
<td>Optional property to compare in each element of the array</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">const array1 = [{name: 'Foo', id: 'a'}, {name: 'Bar', id: 'b'}];
const array2 = [{name: 'Foo', id: 'z'}, {name: 'Zippy', id: 'b'}];
console.log(intersect(array1, array2, 'name'));
// Logs [{name: 'Foo', id: 'a'}]
console.log(intersect(array1, array2, 'id'));
// Logs [{name: 'Bar', id: 'b'}]
</code></pre>
<p><a name="module_array..unique"></a></p>
<h3>unique(arr, [prop]) ⇒ <code>array</code></h3>
<p>Take an array of elements and return an array containing unique elements.
If an element is an object or array:</p>
<ul>
<li>when <code>prop</code> is <em>undefined</em>, uses <code>JSON.stringify()</code> when checking the elements</li>
<li>when <code>prop</code> is <em>provided</em>, only that property needs to match for the element to be considered a duplicate and thus excluded from the returned array</li>
</ul>
<p><strong>Returns</strong>: <code>array</code> - A new filtered array<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>arr</td>
<td><code>array</code></td>
<td>Array to be filtered by uniqueness of elements (or property of elements)</td>
</tr>
<tr>
<td>[prop]</td>
<td><code>any</code></td>
<td>Optional property to be tested if an element in <code>arr</code> is an object or array</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">const array1 = [1, 2, 3, 2, 5, 1];
const uniq = unique(array1);
console.log(uniq);
// Logs: [1, 2, 3, 5]
</code></pre>
<p><a name="module_array..diff"></a></p>
<h3>diff(array1, array2, [prop]) ⇒ <code>array</code></h3>
<p>Return a subset of <code>array1</code>, only including elements that are NOT also in <code>array2</code>. The returned array won’t include any elements from <code>array2</code>.
If an element is an object or array:</p>
<ul>
<li>when <code>prop</code> is <em>undefined</em>, uses <code>JSON.stringify()</code> when performing the comparison on an object or array</li>
<li>when <code>prop</code> is <em>provided</em>, only that property needs to match for the item to be excluded fom the returned array</li>
</ul>
<p><strong>Returns</strong>: <code>array</code> - A filtered array<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>array1</td>
<td><code>array</code></td>
<td>Array for which to return a subset</td>
</tr>
<tr>
<td>array2</td>
<td><code>array</code></td>
<td>Array to use as a comparison</td>
</tr>
<tr>
<td>[prop]</td>
<td><code>string</code></td>
<td>Optional property to be tested if an element in <code>array1</code> is an object or array</td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">const array1 = [1, 2, 3, 4];
const array2 = [2, 3, 5, 6, -1];
console.log(diff(array1, array2));
// Logs: [1, 4]
</code></pre>
<p><a name="module_array..chunk"></a></p>
<h3>chunk(arr, n) ⇒ <code>array</code></h3>
<p>From an array passed into the first argument, create an array of arrays, each one consisting of <code>n</code> items. (The final nested array may have fewer than <code>n</code> items.)</p>
<p><strong>Returns</strong>: <code>array</code> - A new, chunked, array<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>arr</td>
<td><code>array</code></td>
<td>Array to be chunked. This array itself will not be modified.</td>
</tr>
<tr>
<td>n</td>
<td><code>number</code></td>
<td>Number of elements per chunk</td>
</tr>
</tbody>
</table>
<p><a name="module_array..range"></a></p>
<h3>range(a, [b]) ⇒ <code>array</code></h3>
<p>Create an array of numbers from 0 to <code>a</code> - 1 (if <code>b</code> not provided) or from <code>a</code> to <code>b</code> (if <code>b</code> is provided).</p>
<p><strong>Returns</strong>: <code>array</code> - A new array of numbers<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td><code>number</code></td>
<td>The length of the 0-based array to be returned if <code>b</code> is NOT provided; the first number in the array if <code>b</code> IS provided.</td>
</tr>
<tr>
<td>[b]</td>
<td><code>number</code></td>
<td>The (optional) last number of the array.</td>
</tr>
</tbody>
</table>
<p><a name="module_array..pad"></a></p>
<h3>pad(arr, size, value) ⇒ <code>array</code></h3>
<p>Pad an array with <code>value</code> until its length equals <code>size</code></p>
<p><strong>Returns</strong>: <code>array</code> - The array passed to <code>arr</code>, padded<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>arr</td>
<td><code>array</code></td>
<td>Array to pad</td>
</tr>
<tr>
<td>size</td>
<td><code>number</code></td>
<td>Total length of the array after padding it</td>
</tr>
<tr>
<td>value</td>
<td><code>any</code></td>
<td>Value to use for each “padded” element of the array</td>
</tr>
</tbody>
</table>
<p><a name="module_array..sort"></a></p>
<h3>sort(arr, [prop], [options]) ⇒ <code>array</code></h3>
<p>Sort an array with sensible defaults: numbers (or numeric strings) before letters and case and diacritics ignored</p>
<p><strong>Returns</strong>: <code>array</code> - The sorted array<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>arr</td>
<td><code>array</code></td>
<td></td>
<td>Array to sort</td>
</tr>
<tr>
<td>[prop]</td>
<td><code>string</code></td>
<td></td>
<td>If dealing with an array of objects, the property by which to sort</td>
</tr>
<tr>
<td>[options]</td>
<td><code>object</code></td>
<td></td>
<td>Object indicating options to override defaults (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options)</td>
</tr>
<tr>
<td>[options.sensitivity]</td>
<td><code>string</code></td>
<td><code>"base"</code></td>
<td>One of ‘base’, ‘accent’, ‘case’, ‘variant’. Default is ‘base’</td>
</tr>
<tr>
<td>[options.numeric]</td>
<td><code>boolean</code></td>
<td><code>true</code></td>
<td>Whether to treat numeric strings as numbers. Default is true</td>
</tr>
<tr>
<td>[options[…rest]]</td>
<td><code>any</code></td>
<td></td>
<td>Other options (besides sensitivity:‘base’ and numeric: true) per the spec for <code>Intl.Collator.prototype.compare</code></td>
</tr>
</tbody>
</table>
<p><a name="module_color"></a></p>
<h2>color</h2>
<p>ESM Import Example</p>
<pre><code class="language-js">import {rgb2Hex} from 'ksjs'
// or:
import {rgb2Hex} from 'ksjs/color.mjs'
// or:
import {rgb2Hex} from 'ksjs/color.js'
</code></pre>
<p>CJS Require Example</p>
<pre><code class="language-js">const {rgb2Hex} = require('ksjs/color.cjs');
// or:
const {rgb2Hex} = require('ksjs/cjs/color.js');
</code></pre>
<ul>
<li><a href="#module_color">color</a>
<ul>
<li><em>static</em>
<ul>
<li><a href="#module_color.hex2Rgb">hex2Rgb</a></li>
</ul>
</li>
<li><em>inner</em>
<ul>
<li><a href="#module_color..rgb2Hex">rgb2Hex(rgb)</a> ⇒ <code>string</code></li>
<li><a href="#module_color..rgba2Hex">rgba2Hex(rgba)</a> ⇒ <code>string</code></li>
<li><a href="#module_color..rgb2Luminance">rgb2Luminance(rgb)</a> ⇒ <code>number</code></li>
<li><a href="#module_color..getContrastColor">getContrastColor(bgColor, [darkColor], [lightColor])</a> ⇒ <code>string</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<p><a name="module_color.hex2Rgb"></a></p>
<h3>hex2Rgb</h3>
<p>Convert a hex value to an rgb or rgba value</p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>hex</td>
<td><code>string</code></td>
<td>Hex color code in shorthand format (e.g. #333, #333a) or longhand (e.g. #333333, #333333aa)</td>
</tr>
<tr>
<td>[alpha]</td>
<td><code>number</code></td>
<td>Optional number from 0 to 1 to be used with 3- or 6-character hex format</td>
</tr>
</tbody>
</table>
<p><a name="module_color..rgb2Hex"></a></p>
<h3>rgb2Hex(rgb) ⇒ <code>string</code></h3>
<p>Convert an rgb value to a 6-digit hex value. If an <em>rgba</em> value is passed, the opacity is ignored</p>
<p><strong>Returns</strong>: <code>string</code> - Hex value (e.g. <code>#ff780a</code>)<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>rgb</td>
<td><code>string</code> | <code>array</code></td>
<td>either an rgb string such as <code>'rgb(255, 120, 10)'</code> or an rgb array such as <code>[255, 120, 10]</code></td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">rgb2Hex('rgb(255, 136, 0)')
// => '#ff8800'
rgb2Hex([255, 136, 0])
// => '#ff8800'
rgb2Hex('rgba(255, 136, 0, .8)')
// => '#ff8800'
</code></pre>
<p><a name="module_color..rgba2Hex"></a></p>
<h3>rgba2Hex(rgba) ⇒ <code>string</code></h3>
<p>Convert an rgba value to an 8-digit hex value, or an rgb value to a 6-digit hex value</p>
<p><strong>Returns</strong>: <code>string</code> - Hex value (e.g. <code>#ff780a80</code>)<br /></p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>rgba</td>
<td><code>string</code> | <code>array</code></td>
<td>either an rgba string such as <code>'rgba(255, 120, 10, .5)'</code> or an rgba array such as <code>[255, 120, 10, .5]</code></td>
</tr>
</tbody>
</table>
<p><strong>Example</strong></p>
<pre><code class="language-js">rgba2Hex('rgba(255, 136, 0, .8)')
// => '#ff8800cc'
rgba2Hex([255, 136, 0, .8])
// => '#ff8800cc'
rgba2Hex('rgb(255, 136, 0)')
// => '#ff8800'
</code></pre>
<p><a name="module_color..rgb2Luminance"></a></p>
<h3>rgb2Luminance(rgb) ⇒ <code>number</code></h3>
<p>Convert an RGB color to a luminance value. You probably don’t want to use this on its own</p>
<p><strong>Returns</strong>: <code>number</code> - The luminance value<br /></p>
<p><strong>See</strong></p>
<ul>
<li><a href="#module_color..getContrastColor">getContrastColor()</a></li>
<li><a href="https://stackoverflow.com/questions/9733288/how-to-programmatically-calculate-the-contrast-ratio-between-two-colors">StackOverflow</a> for more information</li>
</ul>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>rgb</td>
<td><code>string</code> | <code>array</code></td>
<td>RGB value represented as a string (e.g. <code>rgb(200, 100, 78)</code>) or an array (e.g. <code>[200, 100, 78]</code>)</td>
</tr>
</tbody>
</table>
<p><a name="module_color..getContrastColor"></a></p>
<h3>getContrastColor(bgColor, [darkColor], [lightColor]) ⇒ <code>string</code></h3>