mikel 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +52 -0
- package/index.js +58 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Josemi Juanes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Mikel
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
Mikel is a lightweight implementation of the Mustache templating system in JavaScript, designed to be concise and simple. With less than 60 lines of code, it offers a convenient way to render templates using data.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
You can install Mikel via npm or yarn:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
## Install using npm
|
|
14
|
+
$ npm install mikel
|
|
15
|
+
|
|
16
|
+
## Install using yarn
|
|
17
|
+
$ yarn add mikel
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### `m(template, data)`
|
|
23
|
+
|
|
24
|
+
Render the given template string with the provided data object.
|
|
25
|
+
|
|
26
|
+
- `template` (string): The Mustache template string.
|
|
27
|
+
- `data` (object): The data object containing the values to render.
|
|
28
|
+
|
|
29
|
+
Returns: A string with the rendered output.
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import m from "mikel";
|
|
33
|
+
|
|
34
|
+
const template = "Hello, {{ name }}!";
|
|
35
|
+
const data = {
|
|
36
|
+
name: "World",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const result = m(template, data);
|
|
40
|
+
console.log(result); // Output: "Hello, World!"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Limitations
|
|
44
|
+
|
|
45
|
+
Mikel aims to provide a minimalistic implementation of Mustache templating and currently has the following limitations:
|
|
46
|
+
|
|
47
|
+
- **No support for partials:** Mikel does not support partials, meaning you cannot include separate templates within your main template file.
|
|
48
|
+
- **Basic functionality:** While Mikel supports the core features of Mustache templating, it may lack some advanced features found in other implementations.
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
This project is licensed under the [MIT License](LICENSE).
|
package/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const escapedChars = {
|
|
2
|
+
"&": "&",
|
|
3
|
+
"<": "<",
|
|
4
|
+
">": ">",
|
|
5
|
+
'"': """,
|
|
6
|
+
"'": "'",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const escape = str => {
|
|
10
|
+
return str.toString().replace(/[&<>\"']/g, m => escapedChars[m]);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const get = (ctx, path) => {
|
|
14
|
+
return path === "." ? ctx : (path.split(".").reduce((p, k) => p?.[k], ctx) || "");
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const compile = (tokens, output, ctx, index, section) => {
|
|
18
|
+
let i = index;
|
|
19
|
+
while (i < tokens.length) {
|
|
20
|
+
if (i % 2 === 0) {
|
|
21
|
+
output.push(tokens[i]);
|
|
22
|
+
}
|
|
23
|
+
else if (tokens[i].startsWith("!")) {
|
|
24
|
+
output.push(get(ctx, tokens[i].slice(1).trim()));
|
|
25
|
+
}
|
|
26
|
+
else if (tokens[i].startsWith("#") || tokens[i].startsWith("^")) {
|
|
27
|
+
const t = tokens[i].slice(1).trim();
|
|
28
|
+
const value = get(ctx, t);
|
|
29
|
+
const negate = tokens[i].startsWith("^");
|
|
30
|
+
if (!negate && value && Array.isArray(value)) {
|
|
31
|
+
const j = i + 1;
|
|
32
|
+
value.forEach(item => {
|
|
33
|
+
i = compile(tokens, output, item, j, t);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const includeOutput = (!negate && !!value) || (negate && !!!value);
|
|
38
|
+
i = compile(tokens, includeOutput ? output : [], ctx, i + 1, t);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else if (tokens[i].startsWith("/")) {
|
|
42
|
+
if (tokens[i].slice(1).trim() !== section) {
|
|
43
|
+
throw new Error(`Unmatched section end: {{${tokens[i]}}}`);
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
output.push(escape(get(ctx, tokens[i].trim())));
|
|
49
|
+
}
|
|
50
|
+
i = i + 1;
|
|
51
|
+
}
|
|
52
|
+
return i;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default (str, ctx = {}, output = []) => {
|
|
56
|
+
compile(str.split(/\{\{|\}\}/), output, ctx, 0, "");
|
|
57
|
+
return output.join("").replace(/\n/g, "\\n");
|
|
58
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mikel",
|
|
3
|
+
"description": "Micro mustache implementation with zero dependencies",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Josemi Juanes",
|
|
8
|
+
"email": "hello@josemi.xyz"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": "https://github.com/jmjuanes/mikel",
|
|
12
|
+
"bugs": "https://github.com/jmjuanes/mikel/issues",
|
|
13
|
+
"main": "index.js",
|
|
14
|
+
"module": "index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./index.js",
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "jest test.js"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"mustache",
|
|
24
|
+
"templating"
|
|
25
|
+
],
|
|
26
|
+
"files": [
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE",
|
|
29
|
+
"index.js"
|
|
30
|
+
],
|
|
31
|
+
"babel": {
|
|
32
|
+
"presets": [
|
|
33
|
+
"@babel/preset-env"
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@babel/preset-env": "^7.24.3",
|
|
38
|
+
"jest": "^29.7.0"
|
|
39
|
+
}
|
|
40
|
+
}
|