Skip to content

Commit

Permalink
Optimize object
Browse files Browse the repository at this point in the history
  • Loading branch information
dy committed Mar 7, 2024
1 parent 8c8b39c commit 5ab31b0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
10 changes: 6 additions & 4 deletions feature/number.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { lookup, next, err } from "../src/parse.js"
import { lookup, next, skip, err, cur, idx } from "../src/parse.js"
import { PERIOD, _0, _E, _e, _9 } from "../src/const.js"

// parse number
const num = a => a ? err() : [, (a = +next(c => c === PERIOD || (c >= _0 && c <= _9) || (c === _E || c === _e ? 2 : 0))) != a ? err() : a]
const num = (a, _) => [, (
a = +next(c => (c === PERIOD) || (c >= _0 && c <= _9) || (c === _E || c === _e ? 2 : 0))
) != a ? err() : a]

// .1
lookup[PERIOD] = a => (!a && num())
lookup[PERIOD] = a => !a && num()

// 0-9
for (let i = _0; i <= _9; i++) lookup[i] = num
for (let i = _0; i <= _9; i++) lookup[i] = a => a ? err() : num()
15 changes: 10 additions & 5 deletions feature/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import { PREC_ASSIGN, PREC_SEQ, PREC_TOKEN } from '../src/const.js'
// {a:1, b:2, c:3}
group('{}', PREC_TOKEN)
operator('{}', (a, b) => (
!a ? () => ({}) : // {}
a[0] === ',' ? (a = a.slice(1).map(compile), ctx => Object.fromEntries(a.map(a => a(ctx)))) : // {a:1,b:2}
a[0] === ':' ? (a = compile(a), ctx => Object.fromEntries([a(ctx)])) : // {a:1}
(b = compile(a), ctx => ({ [a]: b(ctx) }))
// {}
!a ? () => ({}) :
// {a:1, b}
a[0] === ',' ? (
a = a.slice(1).map(p => compile(p[0] === ':' ? p : [':', p, p])), ctx => Object.fromEntries(a.map(a => a(ctx)))
) :
// {a:1}, {a}
(a = compile(a[0] === ':' ? a : [':', a, a]), ctx => Object.fromEntries([a(ctx)]))
))

binary(':', PREC_ASSIGN, true)
operator(':', (a, b) => (b = compile(b), a = Array.isArray(a) ? compile(a) : (a => a).bind(0, a), ctx => [a(ctx), b(ctx)]))
// "a": a, a: a
operator(':', (a, b) => (b = compile(b), Array.isArray(a) ? (a = compile(a), ctx => [a(ctx), b(ctx)]) : ctx => [a, b(ctx)]))
6 changes: 1 addition & 5 deletions test/justin.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('Arrays', () => {
is(script('[a]')({ a: 1 }), [1])
})

test('Ops', function(qunit) {
test('Ops', function (qunit) {
is(script('1')(), 1)
is(script('1+2')(), 3)
is(script('1*2')(), 2)
Expand Down Expand Up @@ -198,7 +198,3 @@ test('comment case', () => {
const expr = 'a // skip all this'
is(script(expr)({ a: 'a' }), 'a')
})

test('spread', () => {
is(script('{...a, y:2}')({ a: { x: 1 } }), { x: 1, y: 2 })
})
15 changes: 11 additions & 4 deletions test/subscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@ test('readme', t => {
is(subscript('true === false')(), false) // false
})


test('ext: interpolate string', t => {
is(subscript('a+1')({ a: 1 }), 2)
test('numbers', t => {
is(subscript('1')(), 1)
is(subscript('42')(), 42)
is(subscript('3.14')(), 3.14)
is(subscript('.1')(), .1)
is(subscript('0.1')(), 0.1)
is(subscript('0.1E+3')(), 0.1E+3)
is(subscript('1E-3')(), 1E-3)
})

test('strings', t => {
Expand Down Expand Up @@ -388,7 +393,7 @@ test.skip('ext: ternary', t => {
sameAsJs('a? b?c:d :e', { a: 0, c: 0, d: 1, e: 2 })
})

test('ext: object', async t => {
test('object', async t => {
await import('../feature/object.js')
await import('../feature/ternary.js')

Expand All @@ -399,6 +404,8 @@ test('ext: object', async t => {
sameAsJs('{x: 1+2, y:a(3)}', { a: x => x * 2 })
sameAsJs('{1: 2}')
sameAsJs('{x}', { x: 1 })
sameAsJs('{x, y}', { x: 1, y: 2 })
// sameAsJs('{...x}', { x: 1 })

sameAsJs('{a:b?c:d}', { b: 2, c: 3, d: 4 })
sameAsJs('{a:b?c:d, e:!f?g:h}', { b: 2, c: 3, d: 4, f: 1, g: 2, h: 3 })
Expand Down

0 comments on commit 5ab31b0

Please sign in to comment.