-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
979 lines (850 loc) · 72.6 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
<!DOCTYPE html><html lang="en" class=" js content-loading loading"><head>
<meta charset="utf-8">
<meta name="author" content="stephband">
<meta name="description" content="Documentation for Sparky. Sparky is a live data-binding custom <template> element with render tags, composeable includes, functional views and a DOM renderer that batches changes to the browser frame rate.">
<meta name="viewport" content="width=device-width">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@stephband">
<meta property="og:url" content="https://stephen.band/sparky/">
<meta property="og:title" content="Sparky">
<meta property="og:description" content="Sparky is a live data-binding custom <template> element with render tags, composeable includes, functional views and a DOM renderer that batches changes to the browser frame rate.">
<meta property="og:image" content="https://stephen.band/sparky/images/sparky-logo-bg-500.png">
<title>Sparky</title>
<script title="load">
document.documentElement.className += ' js content-loading loading';
window.addEventListener('DOMContentLoaded', () => document.documentElement.classList.remove('content-loading'));
// Wait for other load handlers to run first - Bolt does a few things on
// load (eg. locateable scrolls) and we want them to run before the loading
// class is removed
window.addEventListener('load', () => window.requestAnimationFrame(() => document.documentElement.classList.remove('loading')));
</script>
<link rel="shortcut icon" href="images/logo-64x64.ico">
<link rel="icon" type="image/png" href="images/logo-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="images/logo-64x64.png" sizes="64x64">
<link rel="icon" type="image/png" href="images/logo-128x128.png" sizes="128x128">
<link rel="stylesheet" href="style.min.css">
<!--link rel="stylesheet" href="style.css" /-->
</head>
<body class="@3-12x-grid grid">
<img class="x-1 3x @1-3x @3-x-2 @3-2x" src="images/sparky-logo-dark.svg">
<header class="x-1 3x @2-6x @3-x-2" id="header">
<h1 class="doctext-01">Sparky <span class="doctext-06">3.1.2</span></h1>
<p class="doctext-08">Sparky is a live data-binding custom <template> element with render tags, composeable includes, functional views and a DOM renderer that batches changes to the browser frame rate.</p>
<ul class="link-index index">
<li><strong>repo:</strong> <a href="https://github.com/cruncher/sparky.git">github.com/cruncher/sparky.git</a></li>
<li><strong>bugs:</strong> <a href="https://github.com/cruncher/sparky/issues">github.com/cruncher/sparky/issues</a></li>
</ul>
</header>
<section class="x-1 3x @1-6x @2-9x @3-x-2 grid" locateable="" id="functions">
<div class="x-1 3x">
<h2 class="doctext-02">Sparky templates</h2>
<!--template-->
</div>
<div class="x-1 3x">
<h3 class="doctext-03">tags.</h3>
<!--template-->
<article class="section-doc-toggle-block doc-toggle-block toggle-block block active" id="section-path-pipe-params">
<h3 class="doctext-05 ">
<code class="function language-js">{[path|pipe:params]}</code>
</h3>
<p>Template tags are made of three parts. Values are read from <code>path</code> in the
scope object, piped through any number of <code>pipe</code> functions, which
(depending on the pipe) may require <code>params</code>, and rendered into the DOM.</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">is</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sparky-template<span class="token punctuation">"</span></span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>fetch:package.json<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
{[ repository.url|prepend:'Repo: '|lowercase ]}
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html">repo: https://github.com/cruncher/sparky.git</code></pre>
<p>Paths are a sort-of superset of JS dot notation, where dashes and numbers are
allowed in property names:</p>
<pre><code class="language-html">{[ items.3 ]}
{[ items.my-object ]}</code></pre>
<p>The root path renders the scope object itself:</p>
<pre><code class="language-html">{[ . ]}</code></pre>
<p>And a trailing <code>.</code> also renders the tag when the object at the path mutates:</p>
<pre><code class="language-html">{[ items.3. ]}
{[ items.my-object. ]}</code></pre>
<p>Sparky renders <code>NaN</code>, <code>null</code> and <code>undefined</code> as empty strings, objects as JSON,
functions as <code>'function(params)'</code>, and <code>Infinity</code> as <code>'∞'</code>. Other values
are converted to strings in the normal way.</p>
<p>Tags placed in an <code><input :value="{[]}"></code> set up two-way data binding.
Sparky is strict about input types. A number will not render into an
input <code>type="text"</code>. A string will not render into an input <code>type="number"</code> or
<code>type="range"</code>.</p>
<p>More importantly, a number typed into an input <code>type="text"</code> will be set as
a string in the data, while the same typed into a <code>type="number"</code> will be set
as a number. Sparky gaurantees your data types.</p>
<p>The built-in type converter pipes facilitate rendering values into inputs of
an unmatched type.</p>
</article>
</div>
<div class="x-1 3x @2-x-4">
<h3 class="doctext-03">functions.</h3>
<!--template-->
<article class="section-doc-toggle-block doc-toggle-block toggle-block block active" id="section-fn">
<h3 class="doctext-05 ">
<code class="function language-js">fn="name:params"</code>
</h3>
<p>A <code>fn</code> attribute declares one or more functions to run on a template.
A <strong>function</strong> is expected to supply an object that Sparky uses to
render template <strong>tags</strong>:</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">is</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sparky-template<span class="token punctuation">"</span></span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>fetch:package.json<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
I am {[title]}.
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html">I am Sparky.</code></pre>
<p>The <code>fn</code> attribute may be declared on any element in a sparky template.
Here we use the built-in functions <code>fetch</code>, <code>get</code> and <code>each</code> to loop over an
array of keywords and generate a list:</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">is</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sparky-template<span class="token punctuation">"</span></span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>fetch:package.json<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>get:keywords each<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>{[.]}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>javascript<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>browser<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span></code></pre>
</article>
</div>
<div class="x-1 3x @2-x-7">
<h3 class="doctext-03">includes.</h3>
<!--template-->
<article class="section-doc-toggle-block doc-toggle-block toggle-block block active" id="section-src">
<h3 class="doctext-05 ">
<code class="function language-js">src="#id"</code>
</h3>
<p>Sparky extends <code><template></code> elements (and <code><use></code> elements in SVG) with a <code>src</code>
attribute, turning them into includes. The <code>src</code> attribute must reference
another template (or sn SVG element). Its rendered content replaces the
referring template:</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>my-template<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>get:keywords each<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>{[.]}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">is</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sparky-template<span class="token punctuation">"</span></span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>fetch:package.json<span class="token punctuation">"</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#my-template<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>javascript<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>browser<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span></code></pre>
<p>Includes may be used inside templates, too:</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">is</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sparky-template<span class="token punctuation">"</span></span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>fetch:package.json<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#my-template<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>javascript<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>browser<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span></code></pre>
<p>The <code>src</code> attribute may contain template tags, making the included template
dependent on rendered data. This is how conditionals are written in Sparky.
Here, where there is no <code>keywords</code> property in <code>package.json</code> the template
with id <code>no-keywords</code> is rendered in place of <code>my-template</code>:</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">is</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>sparky-template<span class="token punctuation">"</span></span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>fetch:package.json<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>{[.keywords|yesno:#my-template,#no-keywords]}<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span></code></pre>
</article>
</div>
</section>
<section class="x-1 3x @1-6x @2-9x @3-x-2 column-grid grid" locateable="" id="built-ins">
<div class="x-1 3x">
<h2 class="doctext-03">built-in functions.</h2>
<!--template-->
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-each">
<h3 class="doctext-05 ">
<a href="#fn-each">
<code class="language-js">each</code>
</a>
</h3>
<p>Clones the DOM node and renders a clone for each value in an array.</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>get:keywords each<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>{[.]}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>javascript<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>browser<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span></code></pre>
<p>Where there are functions declared after <code>each</code> in the attribute, they are run
on each clone.</p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-entries">
<h3 class="doctext-05 ">
<a href="#fn-entries">
<code class="language-js">entries</code>
</a>
</h3>
<p>Maps scope to an array of <code>[key, value]</code> arrays.</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>get:repository entries each<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>{[0]}: {[1]}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>type: git<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>url: https://github.com/cruncher/sparky.git<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span></code></pre>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-fetch">
<h3 class="doctext-05 ">
<a href="#fn-fetch">
<code class="language-js">fetch: url</code>
</a>
</h3>
<p>Fetches and parses a JSON file and uses it as scope to render the node.</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>fetch:package.json<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>{[title]}!<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>Sparky!<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span></code></pre>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-get">
<h3 class="doctext-05 ">
<a href="#fn-get">
<code class="language-js">get: path</code>
</a>
</h3>
<p>Maps scope to the value at <code>path</code> of the current scope:</p>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>a</span> <span class="token attr-name">fn</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>get:repository<span class="token punctuation">"</span></span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>{[ url ]}<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>{[type|capitalise]} repository<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>a</span><span class="token punctuation">></span></span></code></pre>
<pre><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>https://github.com/cruncher/sparky.git<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Git repository<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>a</span><span class="token punctuation">></span></span></code></pre>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-stop">
<h3 class="doctext-05 ">
<a href="#fn-stop">
<code class="language-js">stop</code>
</a>
</h3>
<p>Stops sparkifying the element. No more functions are run, and
the contents of the element are not parsed. Use <code>stop</code>
to ignore blocks of static HTML inside a Sparky template.</p>
</article>
</div>
<div class="x-1 3x @2-x-4 @2-6x @2-columns-2">
<h2 class="doctext-03">built-in pipes.</h2>
<!--template-->
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-is">
<h3 class="doctext-05 ">
<a href="#fn-is">
<code class="language-js">is: value</code>
</a>
</h3>
<p>Returns <code>true</code> where value is strictly equal to <code>value</code>, otherwise <code>false</code>. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-has">
<h3 class="doctext-05 ">
<a href="#fn-has">
<code class="language-js">has: property</code>
</a>
</h3>
<p>Returns <code>true</code> where value is an object with <code>property</code>, otherwise <code>false</code>. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-matches">
<h3 class="doctext-05 ">
<a href="#fn-matches">
<code class="language-js">matches: selector</code>
</a>
</h3>
<p>Renders <code>true</code> if value matches <code>selector</code>. Behaviour is overloaded to accept
different types of <code>selector</code>. Where <code>selector</code> is a RegExp, value is assumed
to be a string and tested against it.</p>
<pre><code class="language-html">{[ .|matches:/abc/ ]} // `true` if value contains 'abc'</code></pre>
<p>Where <code>selector</code> is an Object, value is assumed to be an object and its
properties are matched against those of <code>selector</code>.</p>
<pre><code class="language-html">{[ .|matches:{key: 3} ]} // `true` if value.key is `3`.</code></pre>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-class">
<h3 class="doctext-05 ">
<a href="#fn-class">
<code class="language-js">class</code>
</a>
</h3>
<p>Renders the Class – the name of the constructor – of value. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-type">
<h3 class="doctext-05 ">
<a href="#fn-type">
<code class="language-js">type</code>
</a>
</h3>
<p>Renders <code>typeof</code> value. </p>
</article>
<h3 class="doctext-04">Booleans </h3>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-yesno">
<h3 class="doctext-05 ">
<a href="#fn-yesno">
<code class="language-js">yesno: a, b</code>
</a>
</h3>
<p>Where value is truthy renders <code>a</code>, otherwise <code>b</code>. </p>
</article>
<h3 class="doctext-04">Numbers </h3>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-add">
<h3 class="doctext-05 ">
<a href="#fn-add">
<code class="language-js">add: n</code>
</a>
</h3>
<p>Adds <code>n</code> to value. Behaviour is overloaded to accept various types of ‘n’.
Where <code>n</code> is a number, it is summed with value. So to add 1 to any value:</p>
<pre><code class="language-html">{[ number|add:1 ]}</code></pre>
<p>Where ‘n’ is a duration string in date-like format, value is expected to be a
date and is advanced by the duration. So to advance a date by 18 months:</p>
<pre><code class="language-html">{[ date|add:'0000-18-00' ]}</code></pre>
<p>Where ‘n’ is a duration string in time-like format, value is expected to be a
time and is advanced by the duration. So to put a time back by 1 hour and 20
seconds:</p>
<pre><code class="language-html">{[ time|add:'-01:00:20' ]}</code></pre>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-floor">
<h3 class="doctext-05 ">
<a href="#fn-floor">
<code class="language-js">floor</code>
</a>
</h3>
<p>Floors a number.</p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-root">
<h3 class="doctext-05 ">
<a href="#fn-root">
<code class="language-js">root: n</code>
</a>
</h3>
<p>Returns the <code>n</code>th root of value. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-normalise">
<h3 class="doctext-05 ">
<a href="#fn-normalise">
<code class="language-js">normalise: curve, min, max</code>
</a>
</h3>
<p>Return a value in the nominal range <code>0-1</code> from a value between <code>min</code> and
<code>max</code> mapped to a <code>curve</code>, which is one of <code>linear</code>, <code>quadratic</code>, <code>exponential</code>. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-denormalise">
<h3 class="doctext-05 ">
<a href="#fn-denormalise">
<code class="language-js">denormalise: curve, min, max</code>
</a>
</h3>
<p>Return a value in the range <code>min</code>-<code>max</code> of a value in the range <code>0</code>-<code>1</code>,
reverse mapped to <code>curve</code>, which is one of <code>linear</code>, <code>quadratic</code>, <code>exponential</code>. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-to-db">
<h3 class="doctext-05 ">
<a href="#fn-to-db">
<code class="language-js">to-db</code>
</a>
</h3>
<p>Transforms values in the nominal range <code>0-1</code> to dB scale, and, when used in
two-way binding, transforms them back a number in nominal range. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-to-cartesian">
<h3 class="doctext-05 ">
<a href="#fn-to-cartesian">
<code class="language-js">to-cartesian</code>
</a>
</h3>
<p>Transforms a polar coordinate array to cartesian coordinates. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-to-polar">
<h3 class="doctext-05 ">
<a href="#fn-to-polar">
<code class="language-js">to-polar</code>
</a>
</h3>
<p>Transforms a polar coordinate array to cartesian coordinates. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-floatformat">
<h3 class="doctext-05 ">
<a href="#fn-floatformat">
<code class="language-js">floatformat: n</code>
</a>
</h3>
<p>Transforms numbers to strings with <code>n</code> decimal places. Used for
two-way binding, gaurantees numbers are set on scope. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-floatprecision">
<h3 class="doctext-05 ">
<a href="#fn-floatprecision">
<code class="language-js">floatprecision: n</code>
</a>
</h3>
<p>Converts number to string representing number to precision <code>n</code>. </p>
</article>
<h3 class="doctext-04">Dates </h3>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-add">
<h3 class="doctext-05 ">
<a href="#fn-add">
<code class="language-js">add: yyyy-mm-dd</code>
</a>
</h3>
<p>Adds ISO formatted <code>yyyy-mm-dd</code> to a date value, returning a new date. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-dateformat">
<h3 class="doctext-05 ">
<a href="#fn-dateformat">
<code class="language-js">dateformat: yyyy-mm-dd</code>
</a>
</h3>
<p>Converts an ISO date string, a number (in seconds) or a Date object
to a string date formatted with the symbols:</p>
<pre><code><span class="token operator">-</span> <span class="token template-string"><span class="token string">`'YYYY'`</span></span> years
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'YY'`</span></span> <span class="token number">2</span><span class="token operator">-</span>digit year
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'MM'`</span></span> month<span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">-</span>digit
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'MMM'`</span></span> month<span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">-</span>letter
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'MMMM'`</span></span> month<span class="token punctuation">,</span> full name
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'D'`</span></span> day <span class="token keyword">of</span> week
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'DD'`</span></span> day <span class="token keyword">of</span> week<span class="token punctuation">,</span> two<span class="token operator">-</span>digit
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'ddd'`</span></span> weekday<span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">-</span>letter
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'dddd'`</span></span> weekday<span class="token punctuation">,</span> full name
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'hh'`</span></span> hours
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'mm'`</span></span> minutes
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'ss'`</span></span> seconds
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'sss'`</span></span> seconds <span class="token keyword">with</span> decimals</code></pre></article>
<h3 class="doctext-04">Times </h3>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-add">
<h3 class="doctext-05 ">
<a href="#fn-add">
<code class="language-js">add: duration</code>
</a>
</h3>
<p>Adds <code>duration</code>, an ISO-like time string, to a time value, and
returns a number in seconds.</p>
<pre><code>Add <span class="token number">12</span> hours<span class="token punctuation">:</span>
<span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`html
{[ time|add:'12:00' ]}
`</span></span><span class="token template-string"><span class="token string">``</span></span>
Durations may be negative<span class="token punctuation">.</span> Subtract an hour and a half<span class="token punctuation">:</span>
<span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`html
{[ time|add:'-01:30' ]}
`</span></span><span class="token template-string"><span class="token string">``</span></span>
Numbers <span class="token keyword">in</span> a <span class="token template-string"><span class="token string">`duration`</span></span> string are not limited by the cycles <span class="token keyword">of</span> the clock<span class="token punctuation">.</span>
Add <span class="token number">212</span> seconds<span class="token punctuation">:</span>
<span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`html
{[ time|add:'00:00:212' ]}
`</span></span><span class="token template-string"><span class="token string">``</span></span>
Use <span class="token template-string"><span class="token string">`timeformat`</span></span> to transform the result to a readable format<span class="token punctuation">:</span>
<span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`html
{[ time|add:'12:00'|timeformat:'h hours, mm minutes' ]}
`</span></span><span class="token template-string"><span class="token string">``</span></span>
Not that <span class="token template-string"><span class="token string">`duration`</span></span> must be quoted because it contains <span class="token string">':'</span> characters<span class="token punctuation">.</span>
May be used <span class="token keyword">for</span> two<span class="token operator">-</span>way binding<span class="token punctuation">.</span></code></pre></article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-timeformat">
<h3 class="doctext-05 ">
<a href="#fn-timeformat">
<code class="language-js">timeformat: format</code>
</a>
</h3>
<p>Formats value, which must be an ISO time string or a number in seconds, to
match <code>format</code>, a string that may contain the tokens:</p>
<pre><code><span class="token operator">-</span> <span class="token template-string"><span class="token string">`'±'`</span></span> Sign<span class="token punctuation">,</span> renders <span class="token string">'-'</span> <span class="token keyword">if</span> time is negative<span class="token punctuation">,</span> otherwise nothing
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'Y'`</span></span> Years<span class="token punctuation">,</span> approx<span class="token punctuation">.</span>
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'M'`</span></span> Months<span class="token punctuation">,</span> approx<span class="token punctuation">.</span>
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'MM'`</span></span> Months<span class="token punctuation">,</span> remainder <span class="token keyword">from</span> <span class="token function">years</span> <span class="token punctuation">(</span>max <span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">,</span> approx<span class="token punctuation">.</span>
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'w'`</span></span> Weeks
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'ww'`</span></span> Weeks<span class="token punctuation">,</span> remainder <span class="token keyword">from</span> <span class="token function">months</span> <span class="token punctuation">(</span>max <span class="token number">4</span><span class="token punctuation">)</span>
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'d'`</span></span> Days
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'dd'`</span></span> Days<span class="token punctuation">,</span> remainder <span class="token keyword">from</span> <span class="token function">weeks</span> <span class="token punctuation">(</span>max <span class="token number">7</span><span class="token punctuation">)</span>
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'h'`</span></span> Hours
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'hh'`</span></span> Hours<span class="token punctuation">,</span> remainder <span class="token keyword">from</span> <span class="token function">days</span> <span class="token punctuation">(</span>max <span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">-</span>digit format
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'m'`</span></span> Minutes
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'mm'`</span></span> Minutes<span class="token punctuation">,</span> remainder <span class="token keyword">from</span> <span class="token function">hours</span> <span class="token punctuation">(</span>max <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">-</span>digit format
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'s'`</span></span> Seconds
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'ss'`</span></span> Seconds<span class="token punctuation">,</span> remainder <span class="token keyword">from</span> <span class="token function">minutes</span> <span class="token punctuation">(</span>max <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">-</span>digit format
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'sss'`</span></span> Seconds<span class="token punctuation">,</span> remainder <span class="token keyword">from</span> <span class="token function">minutes</span> <span class="token punctuation">(</span>max <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">,</span> fractional
<span class="token operator">-</span> <span class="token template-string"><span class="token string">`'ms'`</span></span> Milliseconds<span class="token punctuation">,</span> remainder <span class="token keyword">from</span> <span class="token function">seconds</span> <span class="token punctuation">(</span>max <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">-</span>digit format
<span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`html
{[ .|timeformat:'±hh:mm' ]}
-13:57
`</span></span><span class="token template-string"><span class="token string">``</span></span></code></pre></article>
<h3 class="doctext-04">Type converters </h3>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-boolean-string">
<h3 class="doctext-05 ">
<a href="#fn-boolean-string">
<code class="language-js">boolean-string</code>
</a>
</h3>
<p>Transforms booleans to strings and vice versa. May by used for two-way binding. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-float-string">
<h3 class="doctext-05 ">
<a href="#fn-float-string">
<code class="language-js">float-string</code>
</a>
</h3>
<p>Transforms numbers to float strings, and, used for two-way binding,
gaurantees numbers are set on scope. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-floats-string">
<h3 class="doctext-05 ">
<a href="#fn-floats-string">
<code class="language-js">floats-string: separator</code>
</a>
</h3>
<p>Transforms an array of numbers to a string using <code>separator</code>, and,
used for two-way binding, gaurantees an array of numbers is set on scope. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-int-string">
<h3 class="doctext-05 ">
<a href="#fn-int-string">
<code class="language-js">int-string</code>
</a>
</h3>
<p>Transforms numbers to integer strings, and, used for two-way binding,
gaurantees integer numbers are set on scope. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-ints-string">
<h3 class="doctext-05 ">
<a href="#fn-ints-string">
<code class="language-js">ints-string: separator</code>
</a>
</h3>
<p>Transforms an array of numbers to a string of integers seperated with
<code>separator</code>, and, used for two-way binding, gaurantees an array of integer
numbers is set on scope. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-string-float">
<h3 class="doctext-05 ">
<a href="#fn-string-float">
<code class="language-js">string-float</code>
</a>
</h3>
<p>Transforms strings to numbers, and, used for two-way binding,
gaurantees float strings are set on scope. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-string-int">
<h3 class="doctext-05 ">
<a href="#fn-string-int">
<code class="language-js">string-int</code>
</a>
</h3>
<p>Transforms strings to integer numbers, and, used for two-way binding,
gaurantees integer strings are set on scope. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-json">
<h3 class="doctext-05 ">
<a href="#fn-json">
<code class="language-js">json</code>
</a>
</h3>
<p>Transforms objects to json, and used in two-way binding, sets parsed
objects on scope. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-contains">
<h3 class="doctext-05 ">
<a href="#fn-contains">
<code class="language-js">contains: n </code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-equals">
<h3 class="doctext-05 ">
<a href="#fn-equals">
<code class="language-js">equals: n </code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-get">
<h3 class="doctext-05 ">
<a href="#fn-get">
<code class="language-js">get: path </code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-invoke">
<h3 class="doctext-05 ">
<a href="#fn-invoke">
<code class="language-js">invoke: name, args </code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-clamp">
<h3 class="doctext-05 ">
<a href="#fn-clamp">
<code class="language-js">clamp: min, max </code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-log">
<h3 class="doctext-05 ">
<a href="#fn-log">
<code class="language-js">log</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-max">
<h3 class="doctext-05 ">
<a href="#fn-max">
<code class="language-js">max</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-min">
<h3 class="doctext-05 ">
<a href="#fn-min">
<code class="language-js">min</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-sin">
<h3 class="doctext-05 ">
<a href="#fn-sin">
<code class="language-js">sin</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-cos">
<h3 class="doctext-05 ">
<a href="#fn-cos">
<code class="language-js">cos</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-tan">
<h3 class="doctext-05 ">
<a href="#fn-tan">
<code class="language-js">tan</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-asin">
<h3 class="doctext-05 ">
<a href="#fn-asin">
<code class="language-js">asin</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-acos">
<h3 class="doctext-05 ">
<a href="#fn-acos">
<code class="language-js">acos</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-atan">
<h3 class="doctext-05 ">
<a href="#fn-atan">
<code class="language-js">atan</code>
</a>
</h3>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-mod">
<h3 class="doctext-05 ">
<a href="#fn-mod">
<code class="language-js">mod</code>
</a>
</h3>
</article>
<h3 class="doctext-04">Strings </h3>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-append">
<h3 class="doctext-05 ">
<a href="#fn-append">
<code class="language-js">append: string</code>
</a>
</h3>
<p>Returns value + <code>string</code>. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-prepend">
<h3 class="doctext-05 ">
<a href="#fn-prepend">
<code class="language-js">prepend: string</code>
</a>
</h3>
<p>Returns <code>string</code> + value. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-prepad">
<h3 class="doctext-05 ">
<a href="#fn-prepad">
<code class="language-js">prepad: string, n</code>
</a>
</h3>
<p>Prepends value with <code>string</code> until the output is <code>n</code> characters long. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-postpad">
<h3 class="doctext-05 ">
<a href="#fn-postpad">
<code class="language-js">postpad: string, n</code>
</a>
</h3>
<p>Appends value with <code>string</code> until the output is <code>n</code> characters long. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-slugify">
<h3 class="doctext-05 ">
<a href="#fn-slugify">
<code class="language-js">slugify</code>
</a>
</h3>
<p>Returns the slug of value. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-is-in">
<h3 class="doctext-05 ">
<a href="#fn-is-in">
<code class="language-js">is-in: array</code>
</a>
</h3>
<p>Returns <code>true</code> if value is contained in <code>array</code>, otherwise <code>false</code>.</p>
<pre><code><span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`html
{[ path|is-in:[0,1] ]}
`</span></span><span class="token template-string"><span class="token string">``</span></span></code></pre></article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-localise">
<h3 class="doctext-05 ">
<a href="#fn-localise">
<code class="language-js">localise: n</code>
</a>
</h3>
<p>Localises a number to <code>n</code> digits. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-lowercase">
<h3 class="doctext-05 ">
<a href="#fn-lowercase">
<code class="language-js">lowercase</code>
</a>
</h3>
<p>Returns the lowercase string of value. </p>
</article>
<article class="fn-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="fn-pluralise">
<h3 class="doctext-05 ">
<a href="#fn-pluralise">
<code class="language-js">pluralise: str1, str2, lang</code>
</a>
</h3>
<p>Where value is singular in a given <code>lang</code>, retuns <code>str1</code>, otherwise <code>str2</code>. </p>
</article>
</div>
</section>
<section class="x-1 3x @1-6x @2-9x @3-x-2 column-grid grid" locateable="" id="customise">
<div class="x-1 3x @2-5x">
<h2 class="doctext-02">Write a function</h2>
<!--template-->
<h3 class="doctext-04">Sparky.fn</h3><p>Sparky ‘functions’ are view-controllers with access to the node where they are
declared and control over the flow of objects being sent to the renderer.</p>
<pre><code><span class="token keyword">import</span> Sparky <span class="token keyword">from</span> <span class="token string">'./sparky/build/module.js'</span><span class="token punctuation">;</span>
<span class="token comment">// Define fn="my-function:params"</span>
Sparky<span class="token punctuation">.</span><span class="token function">fn</span><span class="token punctuation">(</span><span class="token string">'my-function'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> node<span class="token punctuation">,</span> params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// input is a mappable, filterable, consumable,</span>
<span class="token comment">// stoppable stream of scope objects</span>
<span class="token keyword">return</span> input<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span>scope<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Map scope...</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>Functions are called before a node is mounted. They receive a stream of scopes
and the DOM node, and may return the same stream, or a new stream, or they may
block mounting and rendering altogether. Types of return value are interpreted
as follows:</p>
<ul>
<li><code>Promise</code> - automatically converted to a stream</li>
<li><code>Stream</code> - a stream of scopes</li>
<li><code>undefined</code> - equivalent to returning the input stream</li>
<li><code>false</code> - cancels the mount process</li>
</ul>
<p>The stream returned by the last function declared in the <code>fn</code> attribute is
piped to the renderer. Values in that stream are rendered, and the life
of the renderer is controlled by the state of that stream. Sparky’s streams
come from <a href="https://stephen.band/fn/#stream">stephen.band/fn/#stream</a>.</p>
<h3 class="doctext-03">Examples</h3>
<!--template-->
<h3 class="doctext-04">Examples</h3><p>Push a single scope object to the renderer:</p>
<pre><code><span class="token keyword">import</span> <span class="token punctuation">{</span> register<span class="token punctuation">,</span> Stream <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'./sparky/module.js'</span><span class="token punctuation">;</span>
<span class="token function">register</span><span class="token punctuation">(</span><span class="token string">'my-scope'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>node<span class="token punctuation">,</span> params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Return a stream of one object</span>
<span class="token keyword">return</span> Stream<span class="token punctuation">.</span><span class="token keyword">of</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
text<span class="token punctuation">:</span> <span class="token string">'Hello, Sparky!'</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>Return a promise to push a scope when it is ready:</p>
<pre><code><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">'my-package'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>node<span class="token punctuation">,</span> params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Return a promise</span>
<span class="token keyword">return</span> <span class="token function">fetch</span><span class="token punctuation">(</span><span class="token string">'package.json'</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span> <span class="token operator">=></span> response<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>Push a new scope object to the renderer every second:</p>
<pre><code><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">'my-clock'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>node<span class="token punctuation">,</span> params<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> output <span class="token operator">=</span> Stream<span class="token punctuation">.</span><span class="token keyword">of</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Push a new scope to the renderer once per second</span>
<span class="token keyword">const</span> timer <span class="token operator">=</span> <span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
output<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
time<span class="token punctuation">:</span> window<span class="token punctuation">.</span>performance<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Listen to the input stream, stop the interval</span>
<span class="token comment">// timer when it is stopped</span>
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">clearInterval</span><span class="token punctuation">(</span>timer<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Return the stream</span>
<span class="token keyword">return</span> output<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
</div>
<div class="x-1 3x @2-x-6 @2-4x">
<h3 class="doctext-03">Function helpers</h3>
<!--template-->
<article class="function-doc-toggle-block doc-toggle-block toggle-block block" toggleable="" id="function-delegate-types">
<h3 class="doctext-05 ">
<a href="#function-delegate-types">
<code class="function language-js"><span class="token function">delegate</span><span class="token punctuation">(</span>types<span class="token punctuation">)</span></code>
</a>
</h3>
<p>Create an event delegation controller fn, where <code>types</code> is an object of objects
mapping event types and selectors to listener functions:</p>
<pre><code class="language-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> delegate<span class="token punctuation">,</span> register <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'./sparky/module.js'</span><span class="token punctuation">;</span>
<span class="token function">register</span><span class="token punctuation">(</span><span class="token string">'fn-name'</span><span class="token punctuation">,</span> <span class="token function">delegate</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token string">'click'</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token string">'button'</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span>button<span class="token punctuation">,</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Button was clicked...</span>
<span class="token punctuation">}</span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>To further delegate by the name of the button you could use <code>get</code>
and <code>overload</code> from Fn:</p>
<pre><code class="language-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> delegate<span class="token punctuation">,</span> register <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'./sparky/module.js'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> <span class="token keyword">get</span><span class="token punctuation">,</span> overload <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'./fn/module.js'</span><span class="token punctuation">;</span>
<span class="token function">register</span><span class="token punctuation">(</span><span class="token string">'fn-name'</span><span class="token punctuation">,</span> <span class="token function">delegate</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token string">'click'</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token string">'button[name]'</span><span class="token punctuation">:</span> <span class="token function">overload</span><span class="token punctuation">(</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
<span class="token string">'button-name'</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Button name="button-name" was clicked...</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>(This is a little more efficient than listing all possible button names as
selectors).</p>
</article>
</div>
</section>
<section class="x-1 3x @1-6x @2-9x @3-x-2 column-grid grid" locateable="" id="customise">
<div class="x-1 3x @2-5x">
<h2 class="doctext-02">Write a pipe</h2>
<!--template-->
<h3 class="doctext-04">Sparky.pipe</h3><p>Define a pipe using <code>Sparky.pipe(name, fn)</code>.</p>
<pre><code class="language-js"><span class="token keyword">import</span> Sparky <span class="token keyword">from</span> <span class="token string">'./sparky/build/module.js'</span><span class="token punctuation">;</span>
<span class="token comment">// Define {[.|my-pipe:param]}</span>
Sparky<span class="token punctuation">.</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token string">'my-pipe'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>param<span class="token punctuation">,</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// return something from param and value</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<p>A pipe may take any number of params; the final parameter is the
value being piped frmo the tag scope.</p>
<p>Pipes are called during the render process on the next animation frame
after the value of a tag has changed. It is best not to do anything
too expensive in a pipe to keep the render process fast.</p>
</div>
<div class="x-1 3x @2-x-6 @2-4x">
<h3 class="doctext-03">Examples</h3>
<!--template-->
<h3 class="doctext-04">Examples</h3><pre><code class="language-js"><span class="token keyword">import</span> Sparky <span class="token keyword">from</span> <span class="token string">'./sparky/build/module.js'</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> tax <span class="token operator">=</span> <span class="token number">1.175</span><span class="token punctuation">;</span>
<span class="token comment">// Define {[number|add-tax]}</span>
Sparky<span class="token punctuation">.</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token string">'add-tax'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> value <span class="token operator">*</span> tax<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
</div>
</section>
<section class="x-1 3x @1-6x @2-9x @3-x-2 columns-grid grid" locateable="" id="attributes">
<header class="x-1 3x @1-4x block">
<h2 class="doctext-02">API</h2>
</header>
<div class="x-1 3x">
<!--template-->
</div>
</section>
<aside class="x-1 3x @2-x-2 @2-6x text-9">
<h4 class="doctext-04">A note to the Django enthusiast</h4>
<p>By now, you'll have noticed some similarities between Django and Sparky: template-led views, tags with filters and so on. Yes, Sparky was influenced by Django at <a href="https://cruncher.ch">Cruncher</a>, where we frequently write hybrid back-end/front-end templates, hence the similar syntax. They play quite nice together!</p>
</aside>
<!-- Build document -->
<!-- Document interactivity -->
<script type="module">
import '../bolt/module.js';
import * as Sparky from './module.js';
window.Sparky = Sparky;
</script>
</body></html>