Skip to content

Commit

Permalink
v2.1.0
Browse files Browse the repository at this point in the history
- Added typings for TypeScript.
- Added esm version.
- Updated npm devs deps.
  • Loading branch information
hcodes committed Dec 30, 2018
1 parent 3ca9019 commit 6bf644c
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 5,062 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
gulpfile.js
.*
coverage/**
dist/**
Expand Down
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"parserOptions": {
"ecmaVersion": 6
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"semi": [2, "always"],
Expand Down
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# v2.1.0
- Added typings for TypeScript.
- Added esm version.
- Updated npm devs deps.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2014 Denis Seleznev, hcodes@yandex.ru
Copyright (c) 2018 Denis Seleznev, hcodes@yandex.ru
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
233 changes: 233 additions & 0 deletions dist/jstohtml.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
/*!
* jstohtml
* © 2018 Denis Seleznev
* License: MIT
*
* https://github.com/hcodes/jstohtml/
*/

var isArray = Array.isArray,
toString = Object.prototype.toString,
entityMap = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&#39;',
'/': '&#x2F;'
},
escapeRE = /[&<>"'/]/g,
escapeHtml = function(str) {
return str.replace(escapeRE, function(s) {
return entityMap[s];
});
};

var Engine = {
noClosingTag: [
'img', 'input', 'br', 'embed', 'source',
'link', 'meta', 'area', 'command',
'base', 'col', 'param', 'wbr', 'hr', 'keygen'
],

ignoredKeys: [
'b', // block
'e', // element
'm', // modifier
'c', // content
't', // tagName
'cl', // class
'class' // class
],

/**
* Is plain object?
*
* @param {*} obj
* @returns {boolean}
*/
isPlainObj: function(obj) {
return toString.call(obj) === '[object Object]';
},

/**
* Build a item.
*
* @param {*} data
* @returns {string}
*/
build: function(data) {
if (data === null || data === undefined) {
return '';
}

var buf = [];

if (this.isPlainObj(data)) {
return this.tag(data);
} else if (isArray(data)) {
for (var i = 0, len = data.length; i < len; i++) {
buf.push(this.build(data[i]));
}

return buf.join('');
} else {
return '' + data;
}
},

/**
* Build a tag.
*
* @param {*} data
* @returns {string}
*/
tag: function(data) {
var t = data.t || 'div',
text = '<' + t + this.attrs(data);

if (this.noClosingTag.indexOf(t) !== -1) {
return text + '/>';
}

text += '>';

if (data.c) {
text += this.build(data.c);
}

text += '</' + t + '>';

return text;
},

/**
* Build attrs.
*
* @param {Object} data
* @returns {string}
*/
attrs: function(data) {
var b = data.b,
e = data.e,
m = data.m,
buf = [],
cl = [],
result,
key;

if (b || e) {
b = b || this._currentBlock;
if (e) {
buf.push(this.elem(b, e));
} else {
buf.push(this.block(b));
}

if (this.isPlainObj(m)) {
for (key in m) {
if (m.hasOwnProperty(key)) {
buf.push(this.elem(b, e, key, m[key]));
}
}

buf.sort();
for (var i = 0, len = buf.length; i < len; i++) {
if (buf[i] !== buf[i - 1]) {
cl.push(buf[i]);
}
}
} else {
cl = buf;
}

result = this.attr('class', cl);
this._currentBlock = b;
} else {
cl = data['cl'] || data['class'];
result = cl ? this.attr('class', cl) : '';
}

for (key in data) {
if (data.hasOwnProperty(key) && this.ignoredKeys.indexOf(key) === -1) {
result += this.attr(key, data[key]);
}
}

return result;
},

/**
* Build a attr.
*
* @param {string} name
* @param {*} value
* @returns {string}
*/
attr: function(name, value) {
if (value === undefined || value === null || value === false) {
return '';
}

return ' ' + name + '="' + escapeHtml(isArray(value) ? value.join(' ') : '' + value) + '"';
},

/**
* Build a block.
*
* @param {string} block
* @param {string} [modName]
* @param {*} [modVal]
* @returns {string}
*/
block: function(block, modName, modVal) {
return block + this.mod(modName, modVal);
},

/**
* Build a elem.
*
* @param {string} block
* @param {string} [elemName]
* @param {string} [modName]
* @param {*} [modVal]
* @returns {string}
*/
elem: function(block, elemName, modName, modVal) {

return block + (elemName ? '__' + elemName : '') + this.mod(modName, modVal);
},

/**
* Build a mod.
*
* @param {string} modName
* @param {*} [modVal]
* @returns {string}
*/
mod: function(modName, modVal) {
if (modVal === false || modVal === null || modVal === undefined) {
return '';
}

return '_' + modName + (modVal === '' || modVal === true ? '' : '_' + modVal);
},

/**
* Reset inner properties.
*/
reset: function() {
this._currentBlock = '';
return this;
}
};

/**
* JS to HTML.
*
* @param {*} data
* @returns {string}
*/
export default function jstohtml(data) {
return Engine.reset().build(data);
}
45 changes: 25 additions & 20 deletions dist/jstohtml.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
/*!
* jstohtml v2.0.0
* © 2017 Denis Seleznev
* License: MIT
*
* https://github.com/hcodes/jstohtml/
*/

(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define('jstohtml', [], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.jstohtml = factory();
}
}(this, function() {
'use strict';
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.jstohtml = factory());
}(this, function () { 'use strict';

/*!
* jstohtml
* © 2018 Denis Seleznev
* License: MIT
*
* https://github.com/hcodes/jstohtml/
*/

var isArray = Array.isArray,
toString = Object.prototype.toString,
entityMap = {
Expand Down Expand Up @@ -232,7 +228,16 @@
}
};

return function(data) {
/**
* JS to HTML.
*
* @param {*} data
* @returns {string}
*/
function jstohtml(data) {
return Engine.reset().build(data);
};
}

return jstohtml;

}));
9 changes: 8 additions & 1 deletion dist/jstohtml.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions gulpfile.js

This file was deleted.

18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type JSToHTMLContent = JSToHTMLObject | JSToHTMLObject[] | string | string[] | number | number[] | boolean | boolean[] | RegExp | null;

interface JSToHTMLMod {
[key as string]: boolean | string | number;
}

interface JSToHTMLObject {
b?: string;
e?: string;
m?: JSToHTMLMod;
c?: JSToHTMLContent;
cl?: string | string[];
class?: string | string[];
t?: string;
[key as string]?: boolean | string | number | null;
}

export default function jstohtml(data?: JSToHTMLContent): string;

0 comments on commit 6bf644c

Please sign in to comment.