-
Notifications
You must be signed in to change notification settings - Fork 0
/
hilang.js
104 lines (83 loc) · 2.21 KB
/
hilang.js
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
/*
JavaScript hilang v0.1
Copyright (c) 2018 Arseny Mikhalev
GitHub: ''
License: http://www.opensource.org/licenses/mit-license.php
Hilang - Hiccup-like (Clojure) Domain Specific Language for JavaScript.
* Hiccup - https://github.com/weavejester/hiccup/wiki/Syntax
No dependencies, es5.
Primary use: VueJS template
*/
var hilang = (function() {
'use strict';
function _hilang(_dsl) {
/**
* This function takes dsl and converts it to a single line html.
*
* Expects data (dsl) in the following form:
*
* ['div', {attrs: 'class="main-content"'},
* ['p', 'Example: This is a <strong>sentence</strong> of text and it also has some <em>italicized text</em> too.'],
* ['p', 'This is <a href="/index.html">another paragraph</a>.']];
*/
// [0] Check for null exception
if (_dsl.length === 0) { return; }
// Otherwise proceed with logic
var tag = _dsl[0];
// [1] Check for undefined/null
var posOne =
_dsl[1] ?
_dsl[1]
: []
;
var posOneIsArr = Array.isArray(posOne);
var children = '';
var attributes = '';
// Type 1 dsl -> ['div', 'some strings']
if (typeof posOne === 'string' || posOne instanceof String) {
children = posOne;
}
// Type 2 dsl -> ['div', ['div', 'some strings']]
else if (posOneIsArr) {
var recur2 = _dsl.slice(1);
children =
recur2
.map(function(rec) {
return _hilang(rec);
})
.join('')
;
}
// Type 3 dsl -> ['div', {class: 'has-attributes'}]
else {
attributes = ' ' + _dsl[1].attrs;
var recur3 = _dsl.slice(2);
var strChildren =
recur3.length !== 0 ?
recur3[0]:
''
;
if ( typeof strChildren === 'string' || strChildren instanceof String ) {
children = strChildren;
}
else {
children =
recur3
.map(function(rec) {
return _hilang(rec);
})
.join('')
;
}
}
var _html = '<' + tag + attributes + '>' + children + '</' + tag + '>';
return _html;
}
return _hilang;
})();
(function() {
'use strict';
if (typeof define === 'function' && define.amd) { define('hilang', function() { return hilang; }); }
else if (typeof module !== 'undefined' && module.exports) { module.exports = hilang; }
else { window.hilang = hilang; }
})();