Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
61 changed files
with
47,746 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.nyc_output/ | ||
build/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: node_js | ||
sudo: false | ||
node_js: | ||
- "9" | ||
- "10" | ||
- "node" | ||
install: | ||
- npm install | ||
before_script: | ||
- npm install -g gulp-cli | ||
script: | ||
- gulp prod-test | ||
after_success: | ||
- 'nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Debug current DEV_TEST with Mocha", | ||
"type": "node", | ||
"request": "launch", | ||
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", | ||
"stopOnEntry": false, | ||
"args": ["--no-timeouts", "${relativeFile}"], | ||
"env": { "TEST_MODE": "DEV"}, | ||
"cwd": "${workspaceRoot}" | ||
}, | ||
{ | ||
"name": "Debug current GEN_TEST with Mocha", | ||
"type": "node", | ||
"request": "launch", | ||
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", | ||
"stopOnEntry": false, | ||
"args": ["--no-timeouts", "${relativeFile}"], | ||
"env": { "TEST_MODE": "GEN"}, | ||
"cwd": "${workspaceRoot}" | ||
}, | ||
{ | ||
"name": "Debug current PROD_TEST with Mocha", | ||
"type": "node", | ||
"request": "launch", | ||
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", | ||
"stopOnEntry": false, | ||
"args": ["--no-timeouts", "${relativeFile}"], | ||
"cwd": "${workspaceRoot}" | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,101 @@ | ||
# ez-string | ||
A string template renderer for JavaScript without memory leaks. | ||
A string template renderer for JavaScript without memory leaks. It supports referencing variables by position, by name. One can access properties and array elements. | ||
|
||
## Example | ||
|
||
```js | ||
const render = require('ez-string'); | ||
let book; | ||
|
||
// Format using variable's position | ||
book = render('One of my favorite books is "{0}" by {1}.', 'The Name of the Wind', 'Patrick Rothfuss'); | ||
// book = 'One of my favorite books is "The Name of the Wind" by Patrick Rothfuss.' | ||
|
||
// Format using variable's name | ||
// Variable names must use A-Za-z0-9$_ | ||
book = render('One of my favorite books is "{title}" by {author}.', | ||
{ title: 'The Name of the Wind', author: 'Patrick Rothfuss'}); | ||
// book = 'One of my favorite books is "The Name of the Wind" by Patrick Rothfuss.' | ||
|
||
// Curly braces are escaped by using double braces | ||
let example; | ||
example = render('{{title}}'); | ||
// example = '{title}' | ||
|
||
// One can use array indices | ||
book = render('"{arr[0]}" was first published in {arr[1]}.', | ||
{ arr: ['The Hobbit', 1937]}); | ||
// book = '"The Hobbit" was first published in 1937.' | ||
|
||
// One can use object properties. | ||
// Properties with names consisting of A-Za-z0-9$_ can be accessed using property notation | ||
// Properties with other names can be accessed using index notation | ||
book = render('"{book.title}" was written by {book.author["first name"]} {book.author["last name"]}. It was published in {book.year}.', { | ||
book: { | ||
title: 'Marina', | ||
year : 1999, | ||
author: { | ||
'first name': 'Carlos', | ||
'last name': 'Zafon' | ||
} | ||
} | ||
}); | ||
// book = '"Marina" was written by Carlos Zafon. It was published in 1999.' | ||
|
||
// If a property name contains a quote character or backslash, they need to be escaped. | ||
// Quotes are prepended by a backslash | ||
// Backslashes need to be doubled | ||
example = render('{data["\\\\"]}', { | ||
data: {'\\': 'backslash'} | ||
}); | ||
// example = 'backslash' | ||
example = render('{data["\\""]}', { | ||
data: {'"': 'quote'} | ||
}); | ||
// example = 'quote' | ||
``` | ||
|
||
## Installation | ||
|
||
Using NodeJS: | ||
```shell | ||
$ npm i --save ez-string | ||
``` | ||
|
||
```js | ||
const render = require('ez-string'); | ||
let book; | ||
|
||
// Format using variable's position | ||
book = render('One of the best books by {author} is "{title}".', { | ||
author: 'Stephen King', | ||
title: '11/22/63' | ||
}); | ||
// book = 'One of the best books by Stephen King is "11/22/63".' | ||
``` | ||
|
||
In a browser: | ||
```html | ||
<!-- Load library which is UMD packed --> | ||
<script src="ez-string.js"></script> | ||
|
||
<script> | ||
const render = require('ez-string'); | ||
let book; | ||
// Format using variable's position | ||
book = render('One of the best books by {author} is "{title}".', { | ||
author: 'Stephen King', | ||
title: '11/22/63' | ||
}); | ||
// book = 'One of the best books by Stephen King is "11/22/63".' | ||
</script> | ||
``` | ||
|
||
## Why ez-string | ||
|
||
The most important reason is that this library doesn't leak memory. | ||
|
||
Many of the existing template renderers perform a two-step process. They compile a string template into a JavaScript function and then execute it while passing context data. However, most users of such libraries rarely cache the resulting compiled function. Instead they may compile the same template again and again. Due to inticacies of NodeJS memory garbage collection. Such pattern usually results in a memory leak, as described by [Meteor developers.](https://blog.meteor.com/an-interesting-kind-of-javascript-memory-leak-8b47d2e7f156) | ||
|
||
Library documentation is [here.](https://kirusi.github.io/ez-string/docs/index.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
If you need to disable some rule occasionally, you can do through comments in the code by adding a line: | ||
|
||
// eslint-disable-next-line <rule-name> | ||
|
||
This will disable the rule only for that one line | ||
*/ | ||
{ | ||
"env": | ||
{ | ||
"es6": true, | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"array-bracket-newline": ["error", { "multiline": true }], | ||
"block-spacing": ["error", "always"], | ||
"brace-style": ["error", "stroustrup"], | ||
"comma-spacing": ["error", { "before": false, "after": true }], | ||
"consistent-return": "error", | ||
"curly": ["error", "all"], | ||
"default-case": "error", | ||
"eqeqeq": "error", | ||
"func-call-spacing": ["error", "never"], | ||
"guard-for-in": "error", | ||
"implicit-arrow-linebreak": ["error", "beside"], | ||
"indent": ["error", 4, { "SwitchCase": 1}], | ||
"key-spacing": ["error", {"beforeColon": false, "afterColon": true}], | ||
"keyword-spacing": ["error", {"before": true, "after": true}], | ||
"max-len": ["error", 150], | ||
"multiline-comment-style": ["error", "bare-block"], | ||
"new-parens": "error", | ||
"no-console": "error", | ||
"no-dupe-args": "error", | ||
"no-empty": "error", | ||
"no-fallthrough": "error", | ||
"no-global-assign": "error", | ||
"no-implicit-coercion": "error", | ||
"no-implicit-globals": "error", | ||
"no-invalid-this": "error", | ||
"no-loop-func": "error", | ||
"no-mixed-spaces-and-tabs": "error", | ||
"no-multi-spaces": "error", | ||
"no-multiple-empty-lines": ["error", { "max": 1 }], | ||
"no-new": "error", | ||
"no-process-exit": "error", | ||
"no-redeclare": "error", | ||
"no-return-assign": "error", | ||
"no-return-await": "error", | ||
"no-self-assign": "error", | ||
"no-self-compare": "error", | ||
"no-sequences": "error", | ||
"no-throw-literal": "error", | ||
"no-trailing-spaces": "error", | ||
"no-undef": "error", | ||
"no-unmodified-loop-condition": "error", | ||
"no-unreachable": "error", | ||
"no-unused-expressions": ["error", { | ||
"allowShortCircuit": true, | ||
"allowTernary": true | ||
}], | ||
"no-use-before-define": ["error", { | ||
"functions": true, | ||
"classes": true, | ||
"variables": true | ||
}], | ||
"no-var": "error", | ||
"no-void": "error", | ||
"no-whitespace-before-property": "error", | ||
"quotes": ["error", "single"], | ||
"semi": ["error", "always"], | ||
"semi-spacing": ["error", {"before": false, "after": true}], | ||
"space-before-blocks": "error", | ||
"space-before-function-paren": ["error", "never"], | ||
"space-infix-ops": ["error", {"int32Hint": false}], | ||
"space-in-parens": ["error", "never"], | ||
"space-unary-ops": ["error"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: ['./src/public/ez-string.js'], | ||
output: { | ||
filename: 'ez-string.js', | ||
path: path.resolve(__dirname, '../'), | ||
libraryTarget: 'umd' | ||
} | ||
}; |
Oops, something went wrong.