Skip to content

Commit

Permalink
Minor refactoring and bump version to 0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber623 committed Mar 29, 2018
1 parent fddf76e commit 813ea5f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 53 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ TemplateTemplate is also really tiny:
<tbody>
<tr>
<th>Uncompressed</th>
<td>1,529 bytes</td>
<td>1,436 bytes</td>
</tr>
<tr>
<th>Minified</th>
<td>878 bytes</td>
<td>828 bytes</td>
</tr>
<tr>
<th>Minified and gzipped</th>
<td>493 bytes</td>
<td>486 bytes</td>
</tr>
</tbody>
</table>
Expand Down
44 changes: 21 additions & 23 deletions dist/templatetemplate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* TemplateTemplate 0.1.3
* TemplateTemplate 0.1.4
*
* A very small JavaScript <template> manipulation library.
*
Expand All @@ -11,30 +11,28 @@
*/

(function(root, factory) {
root.TemplateTemplate = factory();
})(this, function() {
root.TemplateTemplate = factory(root.document);
})(this, function(document) {
return function(template, insertions) {
template = template instanceof HTMLElement ? template : document.querySelector(template);
insertions = insertions || {};
if (template instanceof HTMLTemplateElement) {
var importedNode = document.importNode(template.content, true);
Object.entries(insertions).forEach(function(insertionArray) {
var currentNode = importedNode.querySelector(insertionArray[0]), insertionValue = insertionArray[1];
if (insertionValue instanceof Array) {
Object.entries(insertionValue[1]).forEach(function(attributesArray) {
currentNode.setAttribute(attributesArray[0], attributesArray[1]);
});
insertionValue = insertionValue[0];
}
if (insertionValue instanceof DocumentFragment || insertionValue instanceof HTMLElement) {
currentNode.appendChild(insertionValue);
} else {
currentNode.textContent = insertionValue;
}
});
return importedNode;
} else {
throw new TypeError(template.constructor.name + " is not an HTMLTemplateElement.");
if (template === null) {
throw new TypeError(template + " is not an HTMLTemplateElement");
}
var importedNode = document.importNode(template.content, true);
Object.entries(insertions || {}).forEach(function(insertionArray) {
var currentNode = importedNode.querySelector(insertionArray[0]), insertionValue = insertionArray[1];
if (insertionValue instanceof Array) {
Object.entries(insertionValue[1]).forEach(function(attributesArray) {
currentNode.setAttribute(attributesArray[0], attributesArray[1]);
});
insertionValue = insertionValue[0];
}
if (insertionValue instanceof DocumentFragment || insertionValue instanceof HTMLElement) {
currentNode.appendChild(insertionValue);
} else {
currentNode.textContent = insertionValue;
}
});
return importedNode;
};
});
4 changes: 2 additions & 2 deletions dist/templatetemplate.min.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* TemplateTemplate 0.1.3
* TemplateTemplate 0.1.4
*
* A very small JavaScript <template> manipulation library.
*
Expand All @@ -10,4 +10,4 @@
* TemplateTemplate may be freely distributed under the MIT license.
*/

!function(e,t){e.TemplateTemplate=function(){return function(e,t){if(e=e instanceof HTMLElement?e:document.querySelector(e),t=t||{},e instanceof HTMLTemplateElement){var n=document.importNode(e.content,!0);return Object.entries(t).forEach(function(e){var t=n.querySelector(e[0]),o=e[1];o instanceof Array&&(Object.entries(o[1]).forEach(function(e){t.setAttribute(e[0],e[1])}),o=o[0]),o instanceof DocumentFragment||o instanceof HTMLElement?t.appendChild(o):t.textContent=o}),n}throw new TypeError(e.constructor.name+" is not an HTMLTemplateElement.")}}()}(this);
!function(e,t){e.TemplateTemplate=function(e){return function(t,n){if(null===(t=t instanceof HTMLElement?t:e.querySelector(t)))throw new TypeError(t+" is not an HTMLTemplateElement");var r=e.importNode(t.content,!0);return Object.entries(n||{}).forEach(function(e){var t=r.querySelector(e[0]),n=e[1];n instanceof Array&&(Object.entries(n[1]).forEach(function(e){t.setAttribute(e[0],e[1])}),n=n[0]),n instanceof DocumentFragment||n instanceof HTMLElement?t.appendChild(n):t.textContent=n}),r}}(e.document)}(this);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jgarber/templatetemplate",
"version": "0.1.3",
"version": "0.1.4",
"description": "A very small JavaScript <template> manipulation library.",
"main": "dist/templatetemplate.js",
"scripts": {
Expand Down
47 changes: 23 additions & 24 deletions src/templatetemplate.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
(function(root, factory) {
root.TemplateTemplate = factory();
}(this, function() {
root.TemplateTemplate = factory(root.document);
}(this, function(document) {
return function(template, insertions) {
template = template instanceof HTMLElement ? template : document.querySelector(template);
insertions = insertions || {};

if (template instanceof HTMLTemplateElement) {
var importedNode = document.importNode(template.content, true);
if (template === null) {
throw new TypeError(template + ' is not an HTMLTemplateElement');
}

Object.entries(insertions).forEach(function(insertionArray) {
var currentNode = importedNode.querySelector(insertionArray[0]),
insertionValue = insertionArray[1];
var importedNode = document.importNode(template.content, true);

if (insertionValue instanceof Array) {
Object.entries(insertionValue[1]).forEach(function(attributesArray) {
currentNode.setAttribute(attributesArray[0], attributesArray[1]);
});
Object.entries(insertions || {}).forEach(function(insertionArray) {
var currentNode = importedNode.querySelector(insertionArray[0]),
insertionValue = insertionArray[1];

insertionValue = insertionValue[0];
}
if (insertionValue instanceof Array) {
Object.entries(insertionValue[1]).forEach(function(attributesArray) {
currentNode.setAttribute(attributesArray[0], attributesArray[1]);
});

if (insertionValue instanceof DocumentFragment || insertionValue instanceof HTMLElement) {
currentNode.appendChild(insertionValue);
} else {
currentNode.textContent = insertionValue;
}
});
insertionValue = insertionValue[0];
}

return importedNode;
} else {
throw new TypeError(template.constructor.name + ' is not an HTMLTemplateElement.');
}
if (insertionValue instanceof DocumentFragment || insertionValue instanceof HTMLElement) {
currentNode.appendChild(insertionValue);
} else {
currentNode.textContent = insertionValue;
}
});

return importedNode;
};
}));

0 comments on commit 813ea5f

Please sign in to comment.