-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkitten.js
85 lines (67 loc) · 2 KB
/
markitten.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.markitten = {}));
}(this, function (exports) { 'use strict';
/**
*
* @param {string} tagName
* @param {object|string} props
* @param {string[]} kittens
* @returns {string}
*/
function createElement(tagName, props, kittens) {
if (!kittens || !kittens.length) {
return lonelyKitten(tagName, props);
}
return ("<" + tagName + (attributes(props)) + ">" + (litterOfKittens(kittens)) + "</" + tagName + ">")
}
var toString = function () { return '<!-- markitten -->'; };
function lonelyKitten(tagName, props) {
return ("<" + tagName + (attributes(props)) + "/>")
}
function litterOfKittens(kittens) {
return Array.isArray(kittens) ? kittens.join('') : String(kittens);
}
function attributes(props) {
if (typeof props === 'string') {
props = extractIdClassName(props);
}
var result = '';
if (props) {
for (var key in props) {
result += " " + key + "=\"" + (props[key]) + "\"";
}
}
return result;
}
function extractIdClassName(idClassName) {
var ref = idClassName.split('.');
var id = ref[0];
var classNames = ref.slice(1);
if (id[0] === '#') {
id = id.substring(1);
} else {
classNames = [id ].concat( classNames);
id = null;
}
var props = {};
if (id) { props.id = id; }
if (classNames.length) { props.class = classNames.join(' '); }
return props;
}
/**
*
* @param {string} tagName
* @returns {function}
*/
function createFactory(tagName) {
tagName = String(tagName);
var fn = createElement.bind(null, tagName);
fn.toString = lonelyKitten.bind(null, tagName);
return fn;
}
exports.createElement = createElement;
exports.toString = toString;
exports.createFactory = createFactory;
}));