-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.html
9429 lines (8577 loc) · 406 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Ampersand.js, Javascript, ampersand, Node.js, Apps, HTML5">
<title>Amp.js</title>
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Quicksand:500,700&display=swap">
<link rel="stylesheet" href="/documentation/public/css/normalize.min.css">
<link rel="stylesheet" href="/documentation/public/css/main.css">
<script>
!function(g,s,q,r,d){r=g[r]=g[r]||function(){(r.q=r.q||[]).push(
arguments)};d=s.createElement(q);q=s.getElementsByTagName(q)[0];
d.src='//d1l6p2sc9645hc.cloudfront.net/tracker.js';q.parentNode.
insertBefore(d,q)}(window,document,'script','_gs');
_gs('GSN-375569-B', false);
_gs('set', 'cookieDomain', '.ampersandjs.com');
_gs('track');
</script>
</head>
<body class="docs-page">
<nav class="nav-main cf"><a href="/" class="logo logo-ampersand">Amp</a></nav>
<div class="docs cf">
<nav class="nav-docs"><a href="/" class="logo logo-ampersand"></a><a href="#intro" class="section-top">Introduction</a><a href="http://ampersandjs.com" class="section-top external">Ampersand.js</a><a href="https://github.com/ampersandjs/amp" class="section-top external">GitHub Project</a><a href="https://github.com/ampersandjs/amp/issues" class="section-top external">Bugs</a><a href="#sizes" class="section-top external">Filesizes</a><a href="#" class="section-top">dom</a><a href="#amp-add-class" class="section-sub">amp-add-class</a><a href="#amp-has-class" class="section-sub">amp-has-class</a><a href="#amp-remove-class" class="section-sub">amp-remove-class</a><a href="#amp-toggle-class" class="section-sub">amp-toggle-class</a><a href="#" class="section-top">functions</a><a href="#amp-bind" class="section-sub">amp-bind</a><a href="#amp-debounce" class="section-sub">amp-debounce</a><a href="#amp-delay" class="section-sub">amp-delay</a><a href="#amp-limit-calls" class="section-sub">amp-limit-calls</a><a href="#amp-negate" class="section-sub">amp-negate</a><a href="#amp-once" class="section-sub">amp-once</a><a href="#amp-times" class="section-sub">amp-times</a><a href="#" class="section-top">collections</a><a href="#amp-contains" class="section-sub">amp-contains</a><a href="#amp-each" class="section-sub">amp-each</a><a href="#amp-every" class="section-sub">amp-every</a><a href="#amp-filter" class="section-sub">amp-filter</a><a href="#amp-find" class="section-sub">amp-find</a><a href="#amp-invoke" class="section-sub">amp-invoke</a><a href="#amp-map" class="section-sub">amp-map</a><a href="#amp-pluck" class="section-sub">amp-pluck</a><a href="#amp-reduce" class="section-sub">amp-reduce</a><a href="#amp-reject" class="section-sub">amp-reject</a><a href="#amp-sample" class="section-sub">amp-sample</a><a href="#amp-shuffle" class="section-sub">amp-shuffle</a><a href="#amp-size" class="section-sub">amp-size</a><a href="#amp-some" class="section-sub">amp-some</a><a href="#amp-sort-by" class="section-sub">amp-sort-by</a><a href="#amp-to-array" class="section-sub">amp-to-array</a><a href="#" class="section-top">arrays</a><a href="#amp-compact" class="section-sub">amp-compact</a><a href="#amp-difference" class="section-sub">amp-difference</a><a href="#amp-first" class="section-sub">amp-first</a><a href="#amp-flatten" class="section-sub">amp-flatten</a><a href="#amp-index-of" class="section-sub">amp-index-of</a><a href="#amp-intersection" class="section-sub">amp-intersection</a><a href="#amp-jump" class="section-sub">amp-jump</a><a href="#amp-last" class="section-sub">amp-last</a><a href="#amp-range" class="section-sub">amp-range</a><a href="#amp-sorted-index" class="section-sub">amp-sorted-index</a><a href="#amp-sorted-insert" class="section-sub">amp-sorted-insert</a><a href="#amp-union" class="section-sub">amp-union</a><a href="#amp-unique" class="section-sub">amp-unique</a><a href="#" class="section-top">objects</a><a href="#amp-clone" class="section-sub">amp-clone</a><a href="#amp-defaults" class="section-sub">amp-defaults</a><a href="#amp-extend" class="section-sub">amp-extend</a><a href="#amp-has" class="section-sub">amp-has</a><a href="#amp-invert" class="section-sub">amp-invert</a><a href="#amp-keys" class="section-sub">amp-keys</a><a href="#amp-matches" class="section-sub">amp-matches</a><a href="#amp-merge" class="section-sub">amp-merge</a><a href="#amp-omit" class="section-sub">amp-omit</a><a href="#amp-pairs" class="section-sub">amp-pairs</a><a href="#amp-pick" class="section-sub">amp-pick</a><a href="#amp-property" class="section-sub">amp-property</a><a href="#amp-values" class="section-sub">amp-values</a><a href="#" class="section-top">strings</a><a href="#amp-escape" class="section-sub">amp-escape</a><a href="#amp-includes" class="section-sub">amp-includes</a><a href="#amp-to-camel-case" class="section-sub">amp-to-camel-case</a><a href="#amp-trim" class="section-sub">amp-trim</a><a href="#" class="section-top">utils</a><a href="#amp-array-to-readable" class="section-sub">amp-array-to-readable</a><a href="#amp-is-arguments" class="section-sub">amp-is-arguments</a><a href="#amp-is-array" class="section-sub">amp-is-array</a><a href="#amp-is-boolean" class="section-sub">amp-is-boolean</a><a href="#amp-is-date" class="section-sub">amp-is-date</a><a href="#amp-is-empty" class="section-sub">amp-is-empty</a><a href="#amp-is-function" class="section-sub">amp-is-function</a><a href="#amp-is-nan" class="section-sub">amp-is-nan</a><a href="#amp-is-null" class="section-sub">amp-is-null</a><a href="#amp-is-number" class="section-sub">amp-is-number</a><a href="#amp-is-object" class="section-sub">amp-is-object</a><a href="#amp-is-object-equal" class="section-sub">amp-is-object-equal</a><a href="#amp-is-regexp" class="section-sub">amp-is-regexp</a><a href="#amp-is-string" class="section-sub">amp-is-string</a><a href="#amp-is-undefined" class="section-sub">amp-is-undefined</a><a href="#amp-iteratee" class="section-sub">amp-iteratee</a><a href="#amp-random" class="section-sub">amp-random</a><a href="#amp-result" class="section-sub">amp-result</a><a href="#amp-unique-id" class="section-sub">amp-unique-id</a><a href="#" class="section-top">internal</a><a href="#amp-create-callback" class="section-sub">amp-create-callback</a><a href="#amp-internal-flatten" class="section-sub">amp-internal-flatten</a>
</nav>
<div class="docs-content">
<section><a name="intro" href="#intro" class="anchor">
<h1><span class="header-link"></span>Amp — 78 utility functions as individual npm modules</h1></a>
<div class="illustration amp"><img src="/documentation/public/images/ampersand-amp.svg"></div>
<p>This page is the consolidated reference for all the amp modules in a single page for the sake of being 'cmd+f' friendly.</p>
<p>All modules are unit tested in a wide range of browsers (including IE6+) with a CI system composed of <a href="https://saucelabs.com">SauceLabs</a> and <a href="https://travis-ci.org/AmpersandJS/amp">Travis CI</a> by using <a href="https://www.npmjs.org/package/zuul">zuul</a> and <a href="https://www.npmjs.org/package/tape">tape</a>.</p>
<p>Project goals:</p>
<ul>
<li>Excellent, cross-browser test coverage</li>
<li>Independently maintained and versioned</li>
<li>Strict semver for versioning</li>
<li>Try very hard to maintain APIs, ideally staying on <code>1.x.x</code> version</li>
<li>Try to consider the published versions "done" code</li>
<li>Provide easy to remember names (by using namespacing)</li>
<li>Minimize unneeded interdependencies</li>
</ul>
<p>Many of the tests and implementations were ported from <a href="http://underscorejs.org/">Underscore.js</a> in order to benefit from its battle-tested code. Much props to <a href="https://twitter.com/jashkenas">Jeremy Ashkenas</a> and the other underscore contributors. This project would not exist if not for them and underscore's generous MIT license.</p>
</section>
<section><a name="sizes" href="#sizes" class="anchor">
<h2><span class="header-link"></span>How filesizes are calculated</h2></a>
<p>To give you a rough idea of what impact a given module will have on a browserify bundle's filesize we calculate it as follows:</p>
<pre> amp module (browserified, min + gzip)
- empty file (browserified, min + gzip)
----------------------------------------
estimated weight added to your app</pre>
<p>However, this is still a flawed estimate since if you use multiple amp modules, the actual de-duped size will be even small since they likely share dependencies.</p>
</section>
<section class="module"><a name="amp-add-class" href="#amp-add-class" class="anchor">
<h1><span class="header-link"></span>amp-add-class</h1></a>
<div class="module-card">
<h4 class="module-name">amp-add-class</h4>
<p class="module-version">v1.1.0</p>
<p class="module-links"><a href="https://www.npmjs.org/package/amp-add-class">npm</a><a href="https://github.com/ampersandjs/amp/tree/master/modules/add-class">github</a></p>
</div>
<div>
<h3>Docs</h3>
<div><p>Adds class(es) to an element.</p>
<p>All of the following work:</p>
<pre><code class="lang-javascript">addClass(el, 'foo');
addClass(el, 'foo bar');
addClass(el, 'foo', 'bar');
addClass(el, ['foo', 'bar']);
</code></pre>
<p>Optimized for minimal DOM manipulation. The most common usecase of adding a single class will use native <code>Element.classList</code> if available. Otherwise, it will do a single read of <code>el.className</code> and only write back to it once, and only if something was actually changed.</p>
<p><strong>note</strong>: Don't mix-and-match approaches. For example, don't do <code>addClass(el, 'foo', ['bar', 'baz'])</code>. If you need this, flatten it first.</p>
</div>
<h3>Example Usage</h3>
<pre><code class="lang-javascript hljs">var addClass = require('amp-add-class');
var element = document.querySelector('#my-el');
element.outerHTML; //=> '<div>greetings</div>';
// basic adding
addClass(element, 'oh');
element.outerHTML; //=> '<div class="oh">greetings</div>';
// adding an existing class does nothing
addClass(element, 'oh');
// add multiple at once
addClass(element, 'hello', 'there');
element.outerHTML; //=> '<div class="oh hello there">greetings</div>';
// using array
addClass(element, ['foo', 'bar']);
element.outerHTML; //=> '<div class="oh hello there foo bar">greetings</div>';
// add multiple at once with space-separated string
addClass(element, 'baz boo');
element.outerHTML; //=> '<div class="oh hello there baz boo">greetings</div>';
</code></pre>
<div class="code">
<button class="toggle">Show the code</button>
<pre style="display: none"><code class="lang-javascript hljs">var hasClass = require('amp-has-class');
var isString = require('amp-is-string');
var isArray = require('amp-is-array');
var trim = require('amp-trim');
var slice = Array.prototype.slice;
var cleanup = /\s{2,}/g;
var ws = /\s+/;
module.exports = function addClass(el, cls) {
if (arguments.length === 2 && isString(cls)) {
cls = trim(cls).split(ws);
} else {
cls = isArray(cls) ? cls : slice.call(arguments, 1);
}
// optimize for best, most common case
if (cls.length === 1 && el.classList) {
if (cls[0]) el.classList.add(cls[0]);
return el;
}
var toAdd = [];
var i = 0;
var l = cls.length;
var item;
var clsName = el.className;
// see if we have anything to add
for (; i < l; i++) {
item = cls[i];
if (item && !hasClass(clsName, item)) {
toAdd.push(item);
}
}
if (toAdd.length) {
el.className = trim((clsName + ' ' + toAdd.join(' ')).replace(cleanup, ' '));
}
return el;
};
</code></pre>
</div>
<div class="tests">
<button class="toggle">Show the tests</button>
<pre style="display: none"><code class="lang-javascript hljs">var test = require('tape');
var addClass = require('./add-class');
function getEl(className) {
var div = document.createElement('div');
div.className = className;
return div;
}
test('amp-add-class', function (t) {
var el = getEl('oh');
addClass(el, 'oh');
t.equal(el.className, 'oh');
el = getEl('oh');
addClass(el, 'oh');
t.equal(el.className, 'oh', 'adding existing class should do nothing');
addClass(el, 'hello', 'there');
t.equal(el.className, 'oh hello there', 'should be able to add several');
addClass(el, 'oh', 'hello', 'there');
t.equal(el.className, 'oh hello there', 'should never add dupes');
addClass(el, undefined, null, NaN, 0, '');
t.equal(el.className, 'oh hello there', 'should be reasonably tolerant of nonsense');
el = getEl('');
addClass(el, ['oh', 'hello', 'there']);
t.equal(el.className, 'oh hello there', 'should work with arrays too');
el = getEl(' oh hello there ');
var clsName = el.className;
t.equal(clsName, addClass(el, 'hello', 'there').className, 'should not touch classNames if not modifying');
t.equal(addClass(el, 'hello', 'yo').className, 'oh hello there yo', 'should clean up className whitespacing if modifying anyway');
el = getEl('oh');
addClass(el, 'hello there');
t.equal(el.className, 'oh hello there', 'supports adding space separated chars');
el = getEl('oh');
addClass(el, '\t\r\nhello \n there ');
t.equal(el.className, 'oh hello there', 'tolerate extra spaces when adding space separated chars');
t.end();
});
</code></pre>
</div>
<h3>Estimated bundle size increase</h3>
<p>~408 B (<a href="#sizes">details</a>)</p>
<h4>Dependencies</h4>
<div style="display: none">
<ul>
<li><a href="#amp-has-class">amp-has-class</a>
<ul>
<li><a href="#amp-is-string">amp-is-string</a>
</li>
</ul>
</li>
<li><a href="#amp-is-array">amp-is-array</a>
</li>
<li><a href="#amp-is-string">amp-is-string</a>
</li>
<li><a href="#amp-trim">amp-trim</a>
</li>
</ul>
</div>
<div class="dependency-list">
<ul>
<li><a href="#amp-has-class">amp-has-class</a></li>
<li><a href="#amp-is-string">amp-is-string</a></li>
<li><a href="#amp-is-array">amp-is-array</a></li>
<li><a href="#amp-trim">amp-trim</a></li>
</ul>
</div>
</div>
<div></div>
</section>
<section class="module"><a name="amp-has-class" href="#amp-has-class" class="anchor">
<h1><span class="header-link"></span>amp-has-class</h1></a>
<div class="module-card">
<h4 class="module-name">amp-has-class</h4>
<p class="module-version">v1.0.3</p>
<p class="module-links"><a href="https://www.npmjs.org/package/amp-has-class">npm</a><a href="https://github.com/ampersandjs/amp/tree/master/modules/has-class">github</a></p>
</div>
<div>
<h3>Docs</h3>
<div><p>Returns <code>true</code> if the <code>element</code> has the class and <code>false</code> otherwise.</p>
<p>If the first argument is a string, it is assumed to be what you'd get from <code>el.className</code>. This allows it to be used repeatedly for a single element without reading from the DOM more than once.</p>
</div>
<h3>Example Usage</h3>
<pre><code class="lang-javascript hljs">var hasClass = require('amp-has-class');
var el = document.querySelector('#my-div');
el.outerHTML; //=> <div class="oh hi">hello</div>
hasClass(el, 'oh'); //=> true
hasClass(el, 'no'); //=> false
// also works with the string result of `el.className`
var className = el.className;
hasClass(className, 'oh'); //=> true
hasClass(className, 'no'); //=> false
</code></pre>
<div class="code">
<button class="toggle">Show the code</button>
<pre style="display: none"><code class="lang-javascript hljs">var isString = require('amp-is-string');
var whitespaceRE = /[\t\r\n\f]/g;
// note: this is jQuery's approach
module.exports = function hasClass(el, cls) {
var cName = (isString(el) ? el : el.className).replace(whitespaceRE, ' ');
return (' ' + cName + ' ').indexOf(' ' + cls + ' ') !== -1;
};
</code></pre>
</div>
<div class="tests">
<button class="toggle">Show the tests</button>
<pre style="display: none"><code class="lang-javascript hljs">var test = require('tape');
var hasClass = require('./has-class');
function getEl(className) {
var div = document.createElement('div');
div.className = className;
return div;
}
test('amp-has-class', function (t) {
t.ok(hasClass(getEl('hi'), 'hi'));
t.ok(hasClass(getEl('hi there'), 'there'));
t.notOk(hasClass(getEl('hi'), 'something'));
t.notOk(hasClass(getEl('hi there'), 'something'));
t.notOk(hasClass(getEl('hi-there'), 'hi'));
t.notOk(hasClass(getEl('hi-there'), 'there'));
t.notOk(hasClass(getEl('hi-there'), 'i-t'));
t.notOk(hasClass(getEl('hi-there'), '-'));
var className = getEl('hi there').className;
t.ok(hasClass(className, 'hi'));
t.ok(hasClass(className, 'there'));
t.notOk(hasClass(className, 'something'));
t.ok(hasClass(getEl('\thi'), 'hi'), 'can be \t issue #27');
t.ok(hasClass(getEl('\nhi'), 'hi'), 'can be \n issue #27');
t.ok(hasClass(getEl('\rhi'), 'hi'), 'can be \r issue #27');
t.ok(hasClass(getEl('\fhello\fhi\f'), 'hi'), 'can be \f issue #27');
t.end();
});
</code></pre>
</div>
<h3>Estimated bundle size increase</h3>
<p>~131 B (<a href="#sizes">details</a>)</p>
<h4>Dependencies</h4>
<div style="display: none">
<ul>
<li><a href="#amp-is-string">amp-is-string</a>
</li>
</ul>
</div>
<div class="dependency-list">
<ul>
<li><a href="#amp-is-string">amp-is-string</a></li>
</ul>
</div>
</div>
<div></div>
</section>
<section class="module"><a name="amp-remove-class" href="#amp-remove-class" class="anchor">
<h1><span class="header-link"></span>amp-remove-class</h1></a>
<div class="module-card">
<h4 class="module-name">amp-remove-class</h4>
<p class="module-version">v1.1.0</p>
<p class="module-links"><a href="https://www.npmjs.org/package/amp-remove-class">npm</a><a href="https://github.com/ampersandjs/amp/tree/master/modules/remove-class">github</a></p>
</div>
<div>
<h3>Docs</h3>
<div><p>Removes class(es) from an element.</p>
<p>All of the following work:</p>
<pre><code class="lang-javascript">removeClass(el, 'foo');
removeClass(el, 'foo bar');
removeClass(el, 'foo', 'bar');
removeClass(el, ['foo', 'bar']);
</code></pre>
<p>Optimized for minimal DOM manipulation. The most common usecase of removing a single class will use native <code>Element.classList</code> if available. Otherwise, it will do a single read of <code>el.className</code> and only write back to it once, and only if something was actually changed.</p>
<p><strong>note</strong>: Don't mix-and-match approaches. For example, don't do <code>removeClass(el, 'foo', ['bar', 'baz'])</code>. If you need this, flatten it first.</p>
</div>
<h3>Example Usage</h3>
<pre><code class="lang-javascript hljs">var removeClass = require('amp-remove-class');
var element = document.querySelector('#my-el');
element.outerHTML; //=> '<div class="oh hello there">greetings</div>';
removeClass(element, 'oh');
element.outerHTML; //=> '<div class="hello there">greetings</div>';
// remove multiple at once
removeClass(element, 'hello', 'there');
// can be done with a space-separated string
removeClass(element, 'hello there');
// can also be done by passing array
removeClass(element, ['hello', 'there']);
element.outerHTML; //=> '<div class="">greetings</div>';
</code></pre>
<div class="code">
<button class="toggle">Show the code</button>
<pre style="display: none"><code class="lang-javascript hljs">var isString = require('amp-is-string');
var isArray = require('amp-is-array');
var trim = require('amp-trim');
var slice = Array.prototype.slice;
var cleanup = /\s{2,}/g;
var ws = /\s+/;
module.exports = function removeClass(el, cls) {
if (arguments.length === 2 && isString(cls)) {
cls = trim(cls).split(ws);
} else {
cls = isArray(cls) ? cls : slice.call(arguments, 1);
}
// optimize for best, most common case
if (cls.length === 1 && el.classList) {
if (cls[0]) el.classList.remove(cls[0]);
return el;
}
// store two copies
var clsName = ' ' + el.className + ' ';
var result = clsName;
var current;
var start;
for (var i = 0, l = cls.length; i < l; i++) {
current = cls[i];
start = current ? result.indexOf(' ' + current + ' ') : -1;
if (start !== -1) {
start += 1;
result = result.slice(0, start) + result.slice(start + current.length);
}
}
// only write if modified
if (clsName !== result) {
el.className = trim(result.replace(cleanup, ' '));
}
return el;
};
</code></pre>
</div>
<div class="tests">
<button class="toggle">Show the tests</button>
<pre style="display: none"><code class="lang-javascript hljs">var test = require('tape');
var removeClass = require('./remove-class');
function getEl(className) {
var div = document.createElement('div');
div.className = className;
return div;
}
test('amp-remove-class', function (t) {
var el = getEl('oh hello there');
removeClass(el, 'oh');
t.equal(el.className, 'hello there');
removeClass(el, 'oh');
t.equal(el.className, 'hello there', 'removing non-existant class should do nothing');
removeClass(el, 'hello', 'there');
t.equal(el.className, '', 'should be able to remove several');
removeClass(el, 'oh', 'hello', 'there');
t.equal(el.className, '', 'should be ok if doing again');
// reset it
el = getEl('oh hello there');
removeClass(el, undefined, null, NaN, 0, '');
t.equal(el.className, 'oh hello there', 'should be reasonably tolerant of nonsense');
el = getEl('undefined null NaN');
removeClass(el, undefined, null, NaN, 0, '');
t.equal(el.className, 'undefined null NaN', 'should not remove classes named `undefined` etc.');
el = getEl('oh hello there');
t.equal(el.className, 'oh hello there');
removeClass(el, ['oh', 'hello', 'there']);
t.equal(el.className, '');
el = getEl(' oh hello there');
var clsName = el.className;
t.equal(clsName, removeClass(el, 'not', 'changing').className, 'should not touch classNames if not modifying');
t.equal(removeClass(el, 'hello', 'yo').className, 'oh there', 'should clean up classNames whitespacing, if modifying anyway');
el = removeClass(getEl('foobar'), 'foo', 'bar');
t.equal(el.className, 'foobar');
el = getEl('oh hello there');
t.equal(removeClass(el, 'hello there').className, 'oh', 'should support space separated string of classes');
el = getEl('oh hello there');
t.equal(removeClass(el, '\r hello \t \nthere ').className, 'oh', 'should support other weird whitespace when using a string of classes');
t.end();
});
</code></pre>
</div>
<h3>Estimated bundle size increase</h3>
<p>~357 B (<a href="#sizes">details</a>)</p>
<h4>Dependencies</h4>
<div style="display: none">
<ul>
<li><a href="#amp-is-array">amp-is-array</a>
</li>
<li><a href="#amp-is-string">amp-is-string</a>
</li>
<li><a href="#amp-trim">amp-trim</a>
</li>
</ul>
</div>
<div class="dependency-list">
<ul>
<li><a href="#amp-is-array">amp-is-array</a></li>
<li><a href="#amp-is-string">amp-is-string</a></li>
<li><a href="#amp-trim">amp-trim</a></li>
</ul>
</div>
</div>
<div></div>
</section>
<section class="module"><a name="amp-toggle-class" href="#amp-toggle-class" class="anchor">
<h1><span class="header-link"></span>amp-toggle-class</h1></a>
<div class="module-card">
<h4 class="module-name">amp-toggle-class</h4>
<p class="module-version">v1.1.0</p>
<p class="module-links"><a href="https://www.npmjs.org/package/amp-toggle-class">npm</a><a href="https://github.com/ampersandjs/amp/tree/master/modules/toggle-class">github</a></p>
</div>
<div>
<h3>Docs</h3>
<div><p>Toggles the existence of a class on an element. If the class exists it will be removed, if it doesn't it will be added. </p>
<p>If a <code>condition</code> argument is supplied, the truthiness of that condition is what determines whether the class should exist on the element or not. This simplifies common use case of an element needing a class if <code>condition</code> is true. <code>condition</code> can be passed as either a primitive which will eventually coerce to boolean or as a function. If you pass a function, you get current element passed as a first argument.</p>
</div>
<h3>Example Usage</h3>
<pre><code class="lang-javascript hljs">var toggleClass = require('amp-toggle-class');
var element = document.querySelector('#my-el');
element.outerHTML; //=> '<div>greetings</div>';
// if no condition, toggles based on presence
toggleClass(element, 'oh');
element.outerHTML; //=> '<div class="oh">greetings</div>';
// running again, will remove it
toggleClass(element, 'oh');
element.outerHTML; //=> '<div class="">greetings</div>';
// toggling based on condition
// the `condition` always wins.
// Here, the class is missing,
// but condition is falsy
// so `toggleClass` does nothing.
toggleClass(element, 'oh', false);
element.outerHTML; //=> '<div class="">greetings</div>';
</code></pre>
<div class="code">
<button class="toggle">Show the code</button>
<pre style="display: none"><code class="lang-javascript hljs">var isUndefined = require('amp-is-undefined');
var isFunction = require('amp-is-function');
var hasClass = require('amp-has-class');
var addClass = require('amp-add-class');
var removeClass = require('amp-remove-class');
var actions = {
add: addClass,
remove: removeClass
};
module.exports = function toggleClass(el, cls, condition) {
var action;
if (!isUndefined(condition)) {
condition = isFunction(condition) ? condition.call(null, el) : condition;
action = condition ? 'add' : 'remove';
} else {
action = hasClass(el, cls) ? 'remove' : 'add';
}
return actions[action](el, cls);
};
</code></pre>
</div>
<div class="tests">
<button class="toggle">Show the tests</button>
<pre style="display: none"><code class="lang-javascript hljs">var test = require('tape');
var toggleClass = require('./toggle-class');
function getEl(className) {
var div = document.createElement('div');
div.className = className;
return div;
}
test('amp-toggle-class', function (t) {
var el = getEl('oh');
toggleClass(el, 'oh');
t.equal(el.className, '', 'removes if present without condition passed');
toggleClass(el, 'oh');
t.equal(el.className, 'oh', 'puts it back when called again');
el = getEl('oh');
toggleClass(el, 'oh', true);
t.equal(el.className, 'oh', 'should leave it alone if condition is boolean true and class is already present');
el = getEl('oh');
toggleClass(el, 'hi', true);
t.equal(el.className, 'oh hi', 'should add class if condition is boolean true');
el = getEl('');
toggleClass(el, 'oh', false);
t.equal(el.className, '', 'should not remove class if not present and condition is false');
el = getEl('oh');
toggleClass(el, 'oh', false);
t.equal(el.className, '', 'should remove class if condition is boolean false');
// toggling with condition as a booleans
el = getEl('oh');
toggleClass(el, 'hi', function (element) {
t.equal(el, element, 'should pass element to a condition function');
return 1 + 1 === 2;
});
t.equal(el.className, 'oh hi', 'should add class if condition is a function returning true');
el = getEl('oh');
toggleClass(el, 'oh', function (element) {
t.equal(el, element, 'should pass element to a condition function');
return 1 + 1 === 1;
});
t.equal(el.className, '', 'should remove class if condition is a function returning false');
var nonsense = [undefined, null, NaN, 0, ''];
for (var i = 0, l = nonsense.length; i < l; i++) {
el = getEl('ok')
toggleClass(el, nonsense[i]);
t.equal(el.className, 'ok', 'should be reasonably tolerant of nonsense');
}
el = getEl(' oh hello there ');
var clsName = el.className;
t.equal(clsName, toggleClass(el, 'hello', true).className, 'should not touch classNames if not modifying');
// toggling with conditions
el = getEl('oh');
t.equal(el, toggleClass(el, 'hi', true), 'should always return element');
el = getEl('oh');
t.equal(el, toggleClass(el, 'hi', false), 'should always return element');
// toggling without conditions
el = getEl('oh');
t.equal(el, toggleClass(el, 'oh'), 'should always return element');
el = getEl('oh');
t.equal(el, toggleClass(el, 'hi'), 'should always return element');
t.end();
});
</code></pre>
</div>
<h3>Estimated bundle size increase</h3>
<p>~717 B (<a href="#sizes">details</a>)</p>
<h4>Dependencies</h4>
<div style="display: none">
<ul>
<li><a href="#amp-add-class">amp-add-class</a>
<ul>
<li><a href="#amp-has-class">amp-has-class</a>
<ul>
<li><a href="#amp-is-string">amp-is-string</a>
</li>
</ul>
</li>
<li><a href="#amp-is-array">amp-is-array</a>
</li>
<li><a href="#amp-is-string">amp-is-string</a>
</li>
<li><a href="#amp-trim">amp-trim</a>
</li>
</ul>
</li>
<li><a href="#amp-has-class">amp-has-class</a>
<ul>
<li><a href="#amp-is-string">amp-is-string</a>
</li>
</ul>
</li>
<li><a href="#amp-is-function">amp-is-function</a>
</li>
<li><a href="#amp-is-undefined">amp-is-undefined</a>
</li>
<li><a href="#amp-remove-class">amp-remove-class</a>
<ul>
<li><a href="#amp-is-array">amp-is-array</a>
</li>
<li><a href="#amp-is-string">amp-is-string</a>
</li>
<li><a href="#amp-trim">amp-trim</a>
</li>
</ul>
</li>
</ul>
</div>
<div class="dependency-list">
<ul>
<li><a href="#amp-add-class">amp-add-class</a></li>
<li><a href="#amp-has-class">amp-has-class</a></li>
<li><a href="#amp-is-string">amp-is-string</a></li>
<li><a href="#amp-is-array">amp-is-array</a></li>
<li><a href="#amp-trim">amp-trim</a></li>
<li><a href="#amp-is-function">amp-is-function</a></li>
<li><a href="#amp-is-undefined">amp-is-undefined</a></li>
<li><a href="#amp-remove-class">amp-remove-class</a></li>
</ul>
</div>
</div>
<div></div>
</section>
<section class="module"><a name="amp-bind" href="#amp-bind" class="anchor">
<h1><span class="header-link"></span>amp-bind</h1></a>
<div class="module-card">
<h4 class="module-name">amp-bind</h4>
<p class="module-version">v1.0.1</p>
<p class="module-links"><a href="https://www.npmjs.org/package/amp-bind">npm</a><a href="https://github.com/ampersandjs/amp/tree/master/modules/bind">github</a></p>
</div>
<div>
<h3>Lodash Version: <a href="https://www.npmjs.com/package/lodash.bind">lodash.bind</a></h3>
<div>The lodash project now has an independently versioned module that serves the same purpose as this. We recommend you use it for new projects. See <a href="http://blog.andyet.com/2015/03/25/amped-down">this post</a> for more details.</div>
<h3>Docs</h3>
<div><p>Bind a function to an object, meaning that whenever the function is called, the value of <code>this</code> will be the object. Optionally, pass arguments to the function to pre-fill them, also known as partial application.</p>
<p><strong>note:</strong> If you <em>know</em> you're in an environment where you'll have native <code>Function.prototype.bind</code> (such as node.js) there's really no reason not to just use that. It even does the partial application as described above. Many JS runtimes have it these days. Notable exceptions are Phantom.js and IE8 or older.</p>
</div>
<h3>Example Usage</h3>
<pre><code class="lang-javascript hljs">var bind = require('amp-bind');
var obj = {};
var func = function (greeting, who) {
console.log(greeting, who);
console.log(this === obj);
};
var bound = bind(func, obj, 'hello', 'there');
bound();
//=> 'hello there'
//=> true
//Can also act like a partial generator
var partial = bind(func, null, 'hello');
partial('again');
//=> 'hello again'
//=> false
</code></pre>
<div class="code">
<button class="toggle">Show the code</button>
<pre style="display: none"><code class="lang-javascript hljs">var isFunction = require('amp-is-function');
var isObject = require('amp-is-object');
var nativeBind = Function.prototype.bind;
var slice = Array.prototype.slice;
var Ctor = function () {};
module.exports = function bind(func, context) {
var args, bound;
if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!isFunction(func)) throw new TypeError('Bind must be called on a function');
args = slice.call(arguments, 2);
bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
Ctor.prototype = func.prototype;
var self = new Ctor();
Ctor.prototype = null;
var result = func.apply(self, args.concat(slice.call(arguments)));
if (isObject(result)) return result;
return self;
};
return bound;
};
</code></pre>
</div>
<div class="tests">
<button class="toggle">Show the tests</button>
<pre style="display: none"><code class="lang-javascript hljs">var test = require('tape');
var bind = require('./bind');
test('amp-bind', function (t) {
var context = {name : 'moe'};
var func = function(arg) {
return 'name: ' + (this.name || arg);
};
var bound = bind(func, context);
t.equal(bound(), 'name: moe', 'can bind a function to a context');
bound = bind(func, context);
t.equal(bound(), 'name: moe', 'can do OO-style binding');
bound = bind(func, null, 'curly');
t.equal(bound(), 'name: curly', 'can bind without specifying a context');
func = function(salutation, name) {
return salutation + ': ' + name;
};
func = bind(func, this, 'hello');
t.equal(func('moe'), 'hello: moe', 'the function was partially applied in advance');
func = bind(func, this, 'curly');
t.equal(func(), 'hello: curly', 'the function was completely applied in advance');
func = function(salutation, firstname, lastname) {
return salutation + ': ' + firstname + ' ' + lastname;
};
func = bind(func, this, 'hello', 'moe', 'curly');
t.equal(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');
func = function(context, message) {
t.ok(this == context, message);
};
bind(func, 0, 0, 'can bind a function to `0`')();
bind(func, '', '', 'can bind a function to an empty string')();
bind(func, false, false, 'can bind a function to `false`')();
// These tests are only meaningful when using a browser without a native bind function
// To test this with a modern browser, set underscore's nativeBind to undefined
var F = function () { return this; };
var boundf = bind(F, {hello: 'moe curly'});
var Boundf = boundf; // make eslint happy.
var newBoundf = new Boundf();
t.equal(newBoundf.hello, undefined, 'function should not be bound to the context, to comply with ECMAScript 5');
t.equal(boundf().hello, 'moe curly', "When called without the new operator, it's OK to be bound to the context");
t.ok(newBoundf instanceof F, 'a bound instance is an instance of the original function');
t.throws(function() {
bind('notafunction');
}, TypeError, 'throws an error when binding to a non-function');
t.end();
});
</code></pre>
</div>
<h3>Estimated bundle size increase</h3>
<p>~307 B (<a href="#sizes">details</a>)</p>
<h4>Dependencies</h4>
<div style="display: none">
<ul>
<li><a href="#amp-is-function">amp-is-function</a>
</li>
<li><a href="#amp-is-object">amp-is-object</a>
</li>
</ul>
</div>
<div class="dependency-list">
<ul>
<li><a href="#amp-is-function">amp-is-function</a></li>
<li><a href="#amp-is-object">amp-is-object</a></li>
</ul>
</div>
</div>
<div></div>
</section>
<section class="module"><a name="amp-debounce" href="#amp-debounce" class="anchor">
<h1><span class="header-link"></span>amp-debounce</h1></a>
<div class="module-card">
<h4 class="module-name">amp-debounce</h4>
<p class="module-version">v1.0.1</p>
<p class="module-links"><a href="https://www.npmjs.org/package/amp-debounce">npm</a><a href="https://github.com/ampersandjs/amp/tree/master/modules/debounce">github</a></p>
</div>
<div>
<h3>Lodash Version: <a href="https://www.npmjs.com/package/lodash.debounce">lodash.debounce</a></h3>
<div>The lodash project now has an independently versioned module that serves the same purpose as this. We recommend you use it for new projects. See <a href="http://blog.andyet.com/2015/03/25/amped-down">this post</a> for more details.</div>
<h3>Docs</h3>
<div><p>Creates and returns a new debounced version of the passed function which will postpone its execution until after <strong>wait</strong> milliseconds have elapsed since the last time it was invoked. This is useful for implementing behavior that should only happen after something has stopped occuring. For example: validating an input, recalculating a layout after the window has stopped being resized, and so on.</p>
<p>Pass <code>true</code> for the <code>immediate</code> parameter to cause debounce to trigger the function on the leading instead of the trailing edge of the wait interval.</p>
</div>
<h3>Example Usage</h3>
<pre><code class="lang-javascript hljs">var debounce = require('amp-debounce');
var sayHi = function () {
console.log('hi! <3');
};
var debouncedGreeting = debounce(sayHi, 200);
// will only run *once* and only 200ms after last call
debouncedGreeting();
debouncedGreeting();
debouncedGreeting();
debouncedGreeting();
// *200ms pass*
//=> 'hi'
</code></pre>
<div class="code">
<button class="toggle">Show the code</button>
<pre style="display: none"><code class="lang-javascript hljs">var now = Date.now;
// IE < 9
if (!now) {
now = function () {
return (new Date()).valueOf();
};
}
module.exports = function debounce(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function () {
var last = now() - timestamp;
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};
return function () {
context = this;
args = arguments;
timestamp = now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
</code></pre>
</div>
<div class="tests">
<button class="toggle">Show the tests</button>
<pre style="display: none"><code class="lang-javascript hljs">var test = require('tape');
var debounce = require('./debounce');
var delay = require('amp-delay');
test('amp-debounce', function (t) {
t.plan(1);
var counter = 0;
var incr = function () {
counter++;
};
var debouncedIncr = debounce(incr, 64);
debouncedIncr();
debouncedIncr();
delay(debouncedIncr, 32);
delay(function () {
t.equal(counter, 1, 'incr was debounced');
t.end();
}, 500);
});
test('debounce asap', function (t) {
t.plan(4);
var a, b;
var counter = 0;
var incr = function () {
return ++counter;
};
var debouncedIncr = debounce(incr, 100, true);
a = debouncedIncr();
b = debouncedIncr();
t.equal(a, 1);
t.equal(b, 1);
t.equal(counter, 1, 'incr was called immediately');
delay(debouncedIncr, 16);
delay(debouncedIncr, 32);
delay(debouncedIncr, 48);
delay(function(){
t.equal(counter, 1, 'incr was debounced');
t.end();
}, 500);
});
test('debounce asap recursively', function (t) {
t.plan(2);
var counter = 0;
var debouncedIncr = debounce(function(){
counter++;
if (counter < 10) debouncedIncr();
}, 32, true);
debouncedIncr();
t.equal(counter, 1, 'incr was called immediately');
delay(function(){
t.equal(counter, 1, 'incr was debounced');
t.end();
}, 500);
});
test('debounce after system time is set backwards', function (t) {
t.plan(2);
var counter = 0;
var origNowFunc = Date.now;
var debouncedIncr = debounce(function () {
counter++;
}, 50, true);
debouncedIncr();
t.equal(counter, 1, 'incr was called immediately');
var newNow = function () {
return new Date(2013, 0, 1, 1, 1, 1);
};
delay(function() {
debouncedIncr();
t.equal(counter, 2, 'incr was debounced successfully');
t.end();
newNow = origNowFunc;
}, 200);
});
test('debounce re-entrant', function (t) {
t.plan(2);
var sequence = [
['b1', 'b2']
];
var value = '';
var debouncedAppend;
var append = function(arg){
value += this + arg;
var args = sequence.pop();
if (args) {
debouncedAppend.call(args[0], args[1]);
}
};
debouncedAppend = debounce(append, 32);
debouncedAppend.call('a1', 'a2');
t.equal(value, '');
delay(function(){
t.equal(value, 'a1a2b1b2', 'append was debounced successfully');
t.end();
}, 500);
});
</code></pre>
</div>
<h3>Estimated bundle size increase</h3>
<p>~148 B (<a href="#sizes">details</a>)</p>