Skip to content

Commit 9b980c4

Browse files
Add number w/ bool support on values
1 parent 2957b6e commit 9b980c4

File tree

8 files changed

+2151
-804
lines changed

8 files changed

+2151
-804
lines changed

package-lock.json

Lines changed: 2106 additions & 780 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eft-parser",
3-
"version": "0.5.12",
3+
"version": "0.6.0",
44
"description": "ef.js template parser",
55
"main": "dist/eft-parser.min.js",
66
"module": "src/eft-parser.js",
@@ -26,12 +26,12 @@
2626
},
2727
"homepage": "https://github.com/ClassicOldSong/eft-parser#readme",
2828
"devDependencies": {
29-
"chokidar": "^1.7.0",
30-
"eslint": "^4.5.0",
31-
"rollup": "^0.48.2",
32-
"rollup-plugin-browsersync": "^0.2.4",
33-
"rollup-plugin-buble": "^0.15.0",
34-
"rollup-plugin-commonjs": "^8.1.0",
29+
"chokidar": "^2.0.0",
30+
"eslint": "^4.14.0",
31+
"rollup": "^0.53.2",
32+
"rollup-plugin-browsersync": "^0.2.6",
33+
"rollup-plugin-buble": "^0.18.0",
34+
"rollup-plugin-commonjs": "^8.2.6",
3535
"rollup-plugin-eslint": "^4.0.0",
3636
"rollup-plugin-node-resolve": "^3.0.0",
3737
"rollup-plugin-progress": "^0.4.0",

src/eft-parser.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ESCAPE from './escape-parser.js'
1+
import efEscape from './escape-parser.js'
22

33
const typeSymbols = '>#%@.-+'.split('')
44
const reserved = '__EFPLACEHOLDER__ $parent $key $data $element $refs $methods $mount $umount $subscribe $unsubscribe $update $destroy __DIRECTMOUNT__'.split(' ')
@@ -10,6 +10,18 @@ const getErrorMsg = (msg, line = -2) => `Failed to parse eft template: ${msg}. a
1010

1111
const isEmpty = string => !string.replace(/\s/, '')
1212

13+
const checkValidType = obj => ['number', 'boolean', 'string'].indexOf(typeof obj) > -1
14+
15+
const ESCAPE = (string) => {
16+
try {
17+
const parsed = JSON.parse(string)
18+
if (['number', 'boolean'].indexOf(typeof parsed) === -1) return efEscape(string)
19+
return parsed
20+
} catch (e) {
21+
return efEscape(string)
22+
}
23+
}
24+
1325
const getOffset = (string, parsingInfo) => {
1426
if (parsingInfo.offset !== null) return
1527
parsingInfo.offset = string.match(/\s*/)[0]
@@ -58,7 +70,7 @@ const splitDefault = (string) => {
5870
const [_path, ..._default] = string.split('=')
5971
const pathArr = _path.trim().split('.')
6072
const defaultVal = ESCAPE(_default.join('=').trim())
61-
if (defaultVal) return [pathArr, defaultVal]
73+
if (checkValidType(defaultVal)) return [pathArr, defaultVal]
6274
return [pathArr]
6375
}
6476

@@ -67,7 +79,7 @@ const splitLiterals = (string) => {
6779
if (strs.length === 1) return ESCAPE(string)
6880
const tmpl = []
6981
if (strs.length === 2 && !strs[0] && !strs[1]) tmpl.push(0)
70-
else tmpl.push(strs.map(ESCAPE))
82+
else tmpl.push(strs.map(efEscape))
7183
const mustaches = string.match(mustache)
7284
if (mustaches) tmpl.push(...mustaches.map(splitDefault))
7385
return tmpl
@@ -79,7 +91,7 @@ const pushStr = (textArr, str) => {
7991

8092
const parseText = (string) => {
8193
const result = splitLiterals(string)
82-
if (typeof result === 'string') return [result]
94+
if (checkValidType(result)) return [`${result}`]
8395
const [strs, ...exprs] = result
8496
const textArr = []
8597
for (let i = 0; i < exprs.length; i++) {
@@ -276,7 +288,7 @@ const parseLine = ({line, ast, parsingInfo, i}) => {
276288
}
277289

278290
const parseEft = (template) => {
279-
if (!template) throw new TypeError(getErrorMsg('Template required, but nothing present'))
291+
if (!template) throw new TypeError(getErrorMsg('Template required, but nothing given'))
280292
const tplType = typeof template
281293
if (tplType !== 'string') throw new TypeError(getErrorMsg(`Expected a string, but got a(n) ${tplType}`))
282294
const lines = template.split(/\r?\n/)

test/ef-core.dev.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/ef.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/eft-parser.dev.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8">
55
<title>eft-parser Test Page</title>
66
<script src="./eft-parser.dev.js"></script>
7-
<script src="./ef.min.js"></script>
7+
<script src="./ef-core.dev.js"></script>
88
<style type="text/css" media="screen">
99
.box {
1010
border: 1px solid #000;

test/test.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var ef = efCore
2+
13
var template = '\n' +
24
'>div.{{class.text = box test}}\n' +
35
' #id = id1\n' +
@@ -26,7 +28,7 @@ var template = '\n' +
2628
' .One way binded with particle update:\n' +
2729
' >input\n' +
2830
' #type = text\n' +
29-
' %value = 1: {{class.text}} 2: {{class.text2}}\n' +
31+
' %value = "1: {{class.text}} 2: {{class.text2}}"\n' +
3032
' >br\n' +
3133
' >input\n' +
3234
' #type = radio\n' +
@@ -38,7 +40,11 @@ var template = '\n' +
3840
' %checked = {{testRadio2}}\n' +
3941
' >input\n' +
4042
' #type = checkbox\n' +
41-
' %checked = {{testCheck}}\n' +
43+
' %checked = {{testCheck = true}}\n' +
44+
' >input\n' +
45+
' #type = checkbox\n' +
46+
' #disabled = false\n' +
47+
' %checked = {{testCheck = true}}\n' +
4248
' >br\n' +
4349
' .Input style here: \n' +
4450
' >br\n' +
@@ -51,6 +57,9 @@ var template = '\n' +
5157
' >button\n' +
5258
' @click = sendMsg:some data\n' +
5359
' .{{btnText = sendMsg}}\n' +
60+
' >button\n' +
61+
' @click = nothing\n' +
62+
' .Button that does nothing\n' +
5463
' +list'
5564

5665
var template2 = ' this is a comment\n' +
@@ -91,15 +100,15 @@ var data1 = {
91100
}
92101
}
93102

94-
var module1 = new ef(ast)
95-
var module2 = new ef(ast2)
103+
var module1 = ef.create(ast)
104+
var module2 = ef.create(ast2)
96105

97106
ef.inform()
98107

99-
var state = module1.render()
100-
var state2 = module1.render()
101-
var state3 = module2.render()
102-
var state4 = module2.render(data1)
108+
var state = new module1()
109+
var state2 = new module1()
110+
var state3 = new module2()
111+
var state4 = new module2(data1)
103112

104113
state3.list1.push(state4)
105114
state2.branch = state3
@@ -150,7 +159,7 @@ state2.$methods.sendMsg = function (info) {
150159
ef.inform()
151160
var count = parseInt(info.state.$data.style)
152161
var startTime = Date.now()
153-
for (var i = 0; i < count; i++) states.push(module1.render())
162+
for (var i = 0; i < count; i++) states.push(new module1())
154163
state4.list1.push.apply(state4.list1, states)
155164
ef.exec()
156165
var endTime = Date.now()

0 commit comments

Comments
 (0)