Skip to content

Commit 0ce3407

Browse files
author
William Troup
authored
Merge pull request #10 from williamtroup/1.2.0
1.2.0
2 parents 8debf6c + e0c3273 commit 0ce3407

15 files changed

+204
-114
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
JHson.js
33

44
[![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=JHson.js%2C%20a%20free%20JavaScript%json%20converter&url=https://github.com/williamtroup/JHson.js&hashtags=javascript,json,html,converter)
5-
[![npm](https://img.shields.io/badge/npmjs-v1.1.0-blue)](https://www.npmjs.com/package/jhson.js)
6-
[![nuget](https://img.shields.io/badge/nuget-v1.1.0-purple)](https://www.nuget.org/packages/JHson.js/)
5+
[![npm](https://img.shields.io/badge/npmjs-v1.2.0-blue)](https://www.npmjs.com/package/jhson.js)
6+
[![nuget](https://img.shields.io/badge/nuget-v1.2.0-purple)](https://www.nuget.org/packages/JHson.js/)
77
[![license](https://img.shields.io/badge/license-MIT-green)](https://github.com/williamtroup/JHson.js/blob/main/LICENSE.txt)
88
[![discussions Welcome](https://img.shields.io/badge/discussions-Welcome-red)](https://github.com/williamtroup/JHson.js/discussions)
99
[![coded by William Troup](https://img.shields.io/badge/coded_by-William_Troup-yellow)](https://william-troup.com/)
1010
</h1>
1111

1212
> <p align="center">📃 A JavaScript library for converting between HTML and JSON, with binding, templating, attributes, and CSS support.</p>
13-
> <p align="center">v1.1.0</p>
13+
> <p align="center">v1.2.0</p>
1414
<br />
1515
<br />
1616
@@ -22,7 +22,7 @@ JHson.js
2222
- Full support for Attributes, CSS style properties, and formatted text!
2323
- Write your JSON directly to any DOM element for rendering.
2424
- Copy the layout for an entire page without additional files!
25-
- Data templating for text.
25+
- Data templating for text (with defaults support).
2626
- Write CSS directly back to the head for each element!
2727
- Bind JSON directly to DOM elements!
2828
<br />
@@ -83,7 +83,7 @@ Make sure you include the "DOCTYPE html" tag at the top of your HTML, as follows
8383
```markdown
8484
<div id="header" class="header">
8585
<h1>JHson.js</h1>
86-
<p>This is a basic example of how to use <b>JHson.js</b> ... with template data {{template_data}}.</p>
86+
<p>This is a {{template_type | basic}} example of how to use <b>JHson.js</b> ... with template data {{template_data}}.</p>
8787
</div>
8888

8989
<script>
@@ -126,14 +126,14 @@ Make sure you include the "DOCTYPE html" tag at the top of your HTML, as follows
126126
### 4. DOM Element Binding (for writing JSON as HTML):
127127

128128
```markdown
129-
<div data-jhson-options="{ 'json': 'json string' }">
129+
<div data-jhson-js="{ 'json': 'json string' }">
130130
Your HTML.
131131
</div>
132132
```
133133

134-
To see a list of all the available binding options you can use for "data-jhson-options", click [here](docs/binding/OPTIONS.md).
134+
To see a list of all the available binding options you can use for "data-jhson-js", click [here](docs/binding/OPTIONS.md).
135135

136-
To see a list of all the available custom triggers you can use for "data-jhson-options", click [here](docs/binding/CUSTOM_TRIGGERS.md).
136+
To see a list of all the available custom triggers you can use for "data-jhson-js", click [here](docs/binding/CUSTOM_TRIGGERS.md).
137137

138138
<br>
139139

README_NUGET.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# JHson.js v1.1.0
1+
# JHson.js v1.2.0
22

33
[![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=JHson.js%2C%20a%20free%20JavaScript%json%20converter&url=https://github.com/williamtroup/JHson.js&hashtags=javascript,json,html,converter)
4-
[![npm](https://img.shields.io/badge/npmjs-v1.1.0-blue)](https://www.npmjs.com/package/jhson.js)
5-
[![nuget](https://img.shields.io/badge/nuget-v1.1.0-purple)](https://www.nuget.org/packages/JHson.js/)
4+
[![npm](https://img.shields.io/badge/npmjs-v1.2.0-blue)](https://www.npmjs.com/package/jhson.js)
5+
[![nuget](https://img.shields.io/badge/nuget-v1.2.0-purple)](https://www.nuget.org/packages/JHson.js/)
66
[![license](https://img.shields.io/badge/license-MIT-green)](https://github.com/williamtroup/JHson.js/blob/main/LICENSE.txt)
77
[![discussions Welcome](https://img.shields.io/badge/discussions-Welcome-red)](https://github.com/williamtroup/JHson.js/discussions)
88
[![coded by William Troup](https://img.shields.io/badge/coded_by-William_Troup-yellow)](https://william-troup.com/)
@@ -17,7 +17,7 @@
1717
- Full support for Attributes, CSS style properties, and formatted text!
1818
- Write your JSON directly to any DOM element for rendering.
1919
- Copy the layout for an entire page without additional files!
20-
- Data templating for text.
20+
- Data templating for text (with defaults support).
2121
- Write CSS directly back to the head for each element!
2222
- Bind JSON directly to DOM elements!
2323

@@ -64,7 +64,7 @@ Make sure you include the "DOCTYPE html" tag at the top of your HTML, as follows
6464
```markdown
6565
<div id="header" class="header">
6666
<h1>JHson.js</h1>
67-
<p>This is a basic example of how to use <b>JHson.js</b> ... with template data {{template_data}}.</p>
67+
<p>This is a {{template_type | basic}} example of how to use <b>JHson.js</b> ... with template data {{template_data}}.</p>
6868
</div>
6969

7070
<script>
@@ -105,14 +105,14 @@ Make sure you include the "DOCTYPE html" tag at the top of your HTML, as follows
105105
### 4. DOM Element Binding (for writing JSON as HTML):
106106

107107
```markdown
108-
<div data-jhson-options="{ 'json': 'json string' }">
108+
<div data-jhson-js="{ 'json': 'json string' }">
109109
Your HTML.
110110
</div>
111111
```
112112

113-
To see a list of all the available binding options you can use for "data-jhson-options", click [here](docs/binding/OPTIONS.md).
113+
To see a list of all the available binding options you can use for "data-jhson-js", click [here](docs/binding/OPTIONS.md).
114114

115-
To see a list of all the available custom triggers you can use for "data-jhson-options", click [here](docs/binding/CUSTOM_TRIGGERS.md).
115+
To see a list of all the available custom triggers you can use for "data-jhson-js", click [here](docs/binding/CUSTOM_TRIGGERS.md).
116116

117117

118118
### 5. Finishing Up:

dist/jhson.js

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*! JHson.js v1.1.0 | (c) Bunoon 2024 | MIT License */
1+
/*! JHson.js v1.2.0 | (c) Bunoon 2024 | MIT License */
22
(function() {
3-
var _parameter_Document = null, _parameter_Window = null, _parameter_JSON = null, _parameter_Math = null, _public = {}, _configuration = {}, _elements_Type = {}, _string = {empty:"", space:" ", newLine:"\n"}, _json = {text:"#text", cssStyle:"$", attribute:"@", children:"&children",}, _value = {notFound:-1}, _attribute_Name_Options = "data-jhson-options";
3+
var _parameter_Document = null, _parameter_Window = null, _parameter_JSON = null, _parameter_Math = null, _public = {}, _configuration = {}, _elements_Type = {}, _string = {empty:"", space:" ", newLine:"\n", variableStart:"{{", variableEnd:"}}", variableDefault:"|"}, _json = {text:"#text", cssStyle:"$", attribute:"@", children:"&children",}, _value = {notFound:-1}, _attribute_Name_Options = "data-jhson-js";
44
function render() {
55
var tagTypes = _configuration.domElementTypes, tagTypesLength = tagTypes.length;
66
for (var tagTypeIndex = 0; tagTypeIndex < tagTypesLength; tagTypeIndex++) {
@@ -49,7 +49,7 @@
4949
fireCustomTrigger(bindingOptions.onRenderComplete, bindingOptions.element);
5050
}
5151
function buildAttributeOptions(newOptions) {
52-
var options = !isDefinedObject(newOptions) ? {} : newOptions, optionPropertyDefaults = getDefaultHtmlProperties();
52+
var options = getDefaultObject(newOptions, {}), optionPropertyDefaults = getDefaultHtmlProperties();
5353
options.json = getDefaultString(options.json, optionPropertyDefaults.json);
5454
options.templateData = getDefaultObject(options.templateData, optionPropertyDefaults.templateData);
5555
options.removeOriginalAttributes = getDefaultBoolean(options.removeOriginalAttributes, optionPropertyDefaults.removeOriginalAttributes);
@@ -182,13 +182,13 @@
182182
function writeHtml(element, properties) {
183183
if (isDefinedObject(element) && isDefinedString(properties.json)) {
184184
var convertedJsonObject = getObjectFromString(properties.json), writingScope = {css:{}, templateDataKeys:[], templateDataKeysLength:0, templateDataKeysProcessed:[]};
185-
if (properties.clearCssFromHead) {
186-
clearCssStyleTagsFromHead();
187-
}
188-
if (isDefinedObject(properties.templateData)) {
189-
setupWritingScopeTemplateDataKeys(properties, writingScope);
190-
}
191185
if (convertedJsonObject.parsed && isDefinedObject(convertedJsonObject.result)) {
186+
if (properties.clearCssFromHead) {
187+
clearCssStyleTagsFromHead();
188+
}
189+
if (isDefinedObject(properties.templateData)) {
190+
setupWritingScopeTemplateDataKeys(properties, writingScope);
191+
}
192192
for (var key in convertedJsonObject.result) {
193193
if (key === element.nodeName.toLowerCase()) {
194194
if (properties.removeOriginalAttributes) {
@@ -202,12 +202,13 @@
202202
writeNode(element, convertedJsonObject.result[key], properties, writingScope);
203203
}
204204
}
205-
}
206-
if (properties.addCssToHead) {
207-
writeCssStyleTag(writingScope);
208-
}
209-
if (properties.logTemplateDataWarnings) {
210-
checkedForUnusedTemplateData(writingScope);
205+
processRemainingVariablesForDefaults(element);
206+
if (properties.addCssToHead) {
207+
writeCssStyleTag(writingScope);
208+
}
209+
if (properties.logTemplateDataWarnings) {
210+
checkedForUnusedTemplateData(writingScope);
211+
}
211212
}
212213
}
213214
return _public;
@@ -268,10 +269,23 @@
268269
if (writingScope.templateDataKeysLength > 0) {
269270
for (var templateDataKeyIndex = 0; templateDataKeyIndex < writingScope.templateDataKeysLength; templateDataKeyIndex++) {
270271
var templateDataKey = writingScope.templateDataKeys[templateDataKeyIndex];
271-
if (properties.templateData.hasOwnProperty(templateDataKey) && element.innerHTML.indexOf(templateDataKey) > _value.notFound) {
272-
element.innerHTML = replaceAll(element.innerHTML, templateDataKey, properties.templateData[templateDataKey]);
273-
if (writingScope.templateDataKeysProcessed.indexOf(templateDataKey) === _value.notFound) {
274-
writingScope.templateDataKeysProcessed.push(templateDataKey);
272+
if (properties.templateData.hasOwnProperty(templateDataKey)) {
273+
var templateDataKeyReplacement = properties.templateData[templateDataKey];
274+
if (element.innerHTML.indexOf(templateDataKey) > _value.notFound) {
275+
element.innerHTML = replaceAll(element.innerHTML, templateDataKey, templateDataKeyReplacement);
276+
if (writingScope.templateDataKeysProcessed.indexOf(templateDataKey) === _value.notFound) {
277+
writingScope.templateDataKeysProcessed.push(templateDataKey);
278+
}
279+
} else {
280+
templateDataKey = templateDataKey.replace(_string.variableEnd, _string.empty) + _string.space + _string.variableDefault;
281+
var startIndex = element.innerHTML.indexOf(templateDataKey);
282+
if (startIndex > _value.notFound) {
283+
var endIndex = element.innerHTML.indexOf(_string.variableEnd, startIndex);
284+
if (endIndex > _value.notFound) {
285+
var variable = element.innerHTML.substring(startIndex, endIndex + _string.variableEnd.length);
286+
element.innerHTML = replaceAll(element.innerHTML, variable, templateDataKeyReplacement);
287+
}
288+
}
275289
}
276290
}
277291
}
@@ -322,16 +336,28 @@
322336
}
323337
}
324338
}
339+
function processRemainingVariablesForDefaults(element) {
340+
var remainingVariables = getTemplateVariables(element.innerHTML), remainingVariablesLength = remainingVariables.length;
341+
for (var remainingVariableIndex = 0; remainingVariableIndex < remainingVariablesLength; remainingVariableIndex++) {
342+
var variable = remainingVariables[remainingVariableIndex];
343+
if (variable.indexOf(_string.variableDefault) > _value.notFound) {
344+
var defaultValue = variable.replace(_string.variableStart, _string.empty).replace(_string.variableEnd, _string.empty).split(_string.variableDefault)[1];
345+
if (isDefinedString(defaultValue)) {
346+
element.innerHTML = element.innerHTML.replace(variable, defaultValue.trim());
347+
}
348+
}
349+
}
350+
}
325351
function getTemplateVariables(data) {
326352
var result = [];
327353
if (isDefinedString(data)) {
328354
var startIndex = 0, endIndex = 0;
329355
while (startIndex > _value.notFound) {
330-
startIndex = data.indexOf("{{", endIndex);
356+
startIndex = data.indexOf(_string.variableStart, endIndex);
331357
if (startIndex > _value.notFound) {
332-
endIndex = data.indexOf("}}", startIndex);
358+
endIndex = data.indexOf(_string.variableEnd, startIndex);
333359
if (endIndex > _value.notFound) {
334-
var variable = data.substring(startIndex, endIndex + 2);
360+
var variable = data.substring(startIndex, endIndex + _string.variableEnd.length);
335361
result.push(variable);
336362
endIndex += 2;
337363
}
@@ -390,7 +416,7 @@
390416
return data.substring(0, start.length).toLowerCase() === start.toLowerCase();
391417
}
392418
function replaceAll(string, find, replace) {
393-
return string.replace(new RegExp(find, "g"), replace);
419+
return string.replace(new RegExp(find.replace(_string.variableDefault, "[" + _string.variableDefault + "]"), "g"), replace);
394420
}
395421
function getDefaultBoolean(value, defaultValue) {
396422
return isDefinedBoolean(value) ? value : defaultValue;
@@ -532,14 +558,16 @@
532558
};
533559
_public.setConfiguration = function(newConfiguration) {
534560
var configurationChanges = false;
535-
for (var propertyName in newConfiguration) {
536-
if (newConfiguration.hasOwnProperty(propertyName) && _configuration.hasOwnProperty(propertyName) && newConfiguration[propertyName] !== _configuration[propertyName]) {
537-
_configuration[propertyName] = newConfiguration[propertyName];
538-
configurationChanges = true;
561+
if (isDefinedObject(newConfiguration)) {
562+
for (var propertyName in newConfiguration) {
563+
if (newConfiguration.hasOwnProperty(propertyName) && _configuration.hasOwnProperty(propertyName) && newConfiguration[propertyName] !== _configuration[propertyName]) {
564+
_configuration[propertyName] = newConfiguration[propertyName];
565+
configurationChanges = true;
566+
}
567+
}
568+
if (configurationChanges) {
569+
buildDefaultConfiguration(_configuration);
539570
}
540-
}
541-
if (configurationChanges) {
542-
buildDefaultConfiguration(_configuration);
543571
}
544572
return _public;
545573
};
@@ -557,7 +585,7 @@
557585
_configuration.attributeNotSetErrorText = getDefaultString(_configuration.attributeNotSetErrorText, "The attribute '{{attribute_name}}' has not been set correctly.");
558586
}
559587
_public.getVersion = function() {
560-
return "1.1.0";
588+
return "1.2.0";
561589
};
562590
(function(documentObject, windowObject, jsonObject, mathObject) {
563591
_parameter_Document = documentObject;

0 commit comments

Comments
 (0)