mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
fix: math filters now return number, resolves #110
* fix linting
* numbers are no longer fixed:
i.e. {{0.3 | minus 0.1}} now renders as 0.19999
Stick to JavaScript number behavior, see: 8.0.0
This commit is contained in:
+1
-1
@@ -18,6 +18,6 @@
|
|||||||
"prefer-const": 2,
|
"prefer-const": 2,
|
||||||
"no-unused-vars": "off",
|
"no-unused-vars": "off",
|
||||||
"import/export": "off",
|
"import/export": "off",
|
||||||
"@typescript-eslint/no-unused-vars": ["error", { "vars": "all", "args": "off", "ignoreRestSiblings": false }]
|
"@typescript-eslint/no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": false }]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+1927
-1927
File diff suppressed because it is too large
Load Diff
@@ -3,24 +3,12 @@ export default {
|
|||||||
'ceil': (v: number) => Math.ceil(v),
|
'ceil': (v: number) => Math.ceil(v),
|
||||||
'divided_by': (v: number, arg: number) => v / arg,
|
'divided_by': (v: number, arg: number) => v / arg,
|
||||||
'floor': (v: number) => Math.floor(v),
|
'floor': (v: number) => Math.floor(v),
|
||||||
'minus': bindFixed((v: number, arg: number) => v - arg),
|
'minus': (v: number, arg: number) => v - arg,
|
||||||
'modulo': bindFixed((v: number, arg: number) => v % arg),
|
'modulo': (v: number, arg: number) => v % arg,
|
||||||
'round': (v: number, arg: number = 0) => {
|
'round': (v: number, arg: number = 0) => {
|
||||||
const amp = Math.pow(10, arg)
|
const amp = Math.pow(10, arg)
|
||||||
return Math.round(v * amp) / amp
|
return Math.round(v * amp) / amp
|
||||||
},
|
},
|
||||||
'plus': bindFixed((v: number, arg: number) => Number(v) + Number(arg)),
|
'plus': (v: number, arg: number) => Number(v) + Number(arg),
|
||||||
'times': (v: number, arg: number) => v * arg
|
'times': (v: number, arg: number) => v * arg
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFixed (v: number) {
|
|
||||||
const p = String(v).split('.')
|
|
||||||
return (p.length > 1) ? p[1].length : 0
|
|
||||||
}
|
|
||||||
|
|
||||||
function bindFixed (cb: (v: number, arg: number) => number) {
|
|
||||||
return (l: number, r: number) => {
|
|
||||||
const f = Math.max(getFixed(l), getFixed(r))
|
|
||||||
return cb(l, r).toFixed(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
import { deprecate } from '../util/deprecate'
|
|
||||||
|
|
||||||
export abstract class Drop {
|
export abstract class Drop {
|
||||||
valueOf(): any {
|
valueOf (): any {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,8 +64,8 @@ export function parseExp (exp: string, scope: Scope): any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((match = exp.match(lexical.rangeLine))) {
|
if ((match = exp.match(lexical.rangeLine))) {
|
||||||
const low = parseValue(match[1], scope)
|
const low = evalValue(match[1], scope)
|
||||||
const high = parseValue(match[2], scope)
|
const high = evalValue(match[2], scope)
|
||||||
return range(low, high + 1)
|
return range(low, high + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const reported:{[key: string]: boolean} = {}
|
const reported:{[key: string]: boolean} = {}
|
||||||
|
|
||||||
export function deprecate(msg: string, issue: number) {
|
export function deprecate (msg: string, issue: number) {
|
||||||
if (reported[msg]) return
|
if (reported[msg]) return
|
||||||
console.warn(msg + ` See: https://github.com/harttle/liquidjs/issues/${issue}`)
|
console.warn(msg + ` See: https://github.com/harttle/liquidjs/issues/${issue}`)
|
||||||
reported[msg] = true
|
reported[msg] = true
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-15
@@ -96,21 +96,7 @@ export function isObject (value: any): value is object {
|
|||||||
return value !== null && (type === 'object' || type === 'function')
|
return value !== null && (type === 'object' || type === 'function')
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
export function range (start: number, stop: number, step: number = 1) {
|
||||||
* A function to create flexibly-numbered lists of integers,
|
|
||||||
* handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1.
|
|
||||||
* Returns a list of integers from start (inclusive) to stop (exclusive),
|
|
||||||
* incremented (or decremented) by step, exclusive.
|
|
||||||
* Note that ranges that stop before they start are considered to be zero-length instead of
|
|
||||||
* negative — if you'd like a negative range, use a negative step.
|
|
||||||
*/
|
|
||||||
export function range (start: number, stop?: number, step?: number) {
|
|
||||||
if (stop === undefined) {
|
|
||||||
stop = start
|
|
||||||
start = 0
|
|
||||||
}
|
|
||||||
step = step || 1
|
|
||||||
|
|
||||||
const arr: number[] = []
|
const arr: number[] = []
|
||||||
for (let i = start; i < stop; i += step) {
|
for (let i = start; i < stop; i += step) {
|
||||||
arr.push(i)
|
arr.push(i)
|
||||||
|
|||||||
+2
-3
@@ -6,10 +6,10 @@ use(chaiAsPromised)
|
|||||||
|
|
||||||
class SettingsDrop extends Liquid.Types.Drop {
|
class SettingsDrop extends Liquid.Types.Drop {
|
||||||
foo: string = 'FOO'
|
foo: string = 'FOO'
|
||||||
bar() {
|
bar () {
|
||||||
return 'BAR'
|
return 'BAR'
|
||||||
}
|
}
|
||||||
liquidMethodMissing(key: string) {
|
liquidMethodMissing (key: string) {
|
||||||
return key.toUpperCase()
|
return key.toUpperCase()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,6 @@ describe('drop', function () {
|
|||||||
engine = new Liquid()
|
engine = new Liquid()
|
||||||
})
|
})
|
||||||
it('should support liquidMethodMissing', async function () {
|
it('should support liquidMethodMissing', async function () {
|
||||||
let i = 0
|
|
||||||
const src = `{{settings.foo}},{{settings.bar}},{{settings.coo}}`
|
const src = `{{settings.foo}},{{settings.bar}},{{settings.coo}}`
|
||||||
const html = await engine.parseAndRender(src, { settings })
|
const html = await engine.parseAndRender(src, { settings })
|
||||||
return expect(html).to.equal('FOO,BAR,COO')
|
return expect(html).to.equal('FOO,BAR,COO')
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { test } from '../../../stub/render'
|
import { expect } from 'chai'
|
||||||
|
import { test, liquid } from '../../../stub/render'
|
||||||
|
|
||||||
describe('filters/math', function () {
|
describe('filters/math', function () {
|
||||||
describe('abs', function () {
|
describe('abs', function () {
|
||||||
@@ -35,8 +36,10 @@ describe('filters/math', function () {
|
|||||||
describe('modulo', function () {
|
describe('modulo', function () {
|
||||||
it('should return "1" for 3,2', () => test('{{ 3 | modulo: 2 }}', '1'))
|
it('should return "1" for 3,2', () => test('{{ 3 | modulo: 2 }}', '1'))
|
||||||
it('should return "3" for 24,7', () => test('{{ 24 | modulo: 7 }}', '3'))
|
it('should return "3" for 24,7', () => test('{{ 24 | modulo: 7 }}', '3'))
|
||||||
it('should return "3.357" for 183.357,12',
|
it('should return "3.357" for 183.357,12', async () => {
|
||||||
() => test('{{ 183.357 | modulo: 12 }}', '3.357'))
|
const html = await liquid.parseAndRender('{{ 183.357 | modulo: 12 }}')
|
||||||
|
expect(Number(html)).to.be.closeTo(3.357, 0.001)
|
||||||
|
})
|
||||||
it('should convert string', () => test('{{ "24" | modulo: "7" }}', '3'))
|
it('should convert string', () => test('{{ "24" | modulo: "7" }}', '3'))
|
||||||
})
|
})
|
||||||
describe('plus', function () {
|
describe('plus', function () {
|
||||||
|
|||||||
@@ -7,29 +7,29 @@ describe('drop/drop', function () {
|
|||||||
|
|
||||||
class CustomDrop extends Liquid.Types.Drop {
|
class CustomDrop extends Liquid.Types.Drop {
|
||||||
name: string = 'NAME'
|
name: string = 'NAME'
|
||||||
getName() {
|
getName () {
|
||||||
return 'GETNAME'
|
return 'GETNAME'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class CustomDropWithMethodMissing extends CustomDrop {
|
class CustomDropWithMethodMissing extends CustomDrop {
|
||||||
liquidMethodMissing(key: string) {
|
liquidMethodMissing (key: string) {
|
||||||
return key.toUpperCase()
|
return key.toUpperCase()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
it('should call corresponding method', async function () {
|
it('should call corresponding method', async function () {
|
||||||
const html = await liquid.parseAndRender(`{{obj.getName}}`, {obj: new CustomDrop()})
|
const html = await liquid.parseAndRender(`{{obj.getName}}`, { obj: new CustomDrop() })
|
||||||
expect(html).to.equal('GETNAME')
|
expect(html).to.equal('GETNAME')
|
||||||
})
|
})
|
||||||
it('should read corresponding property', async function () {
|
it('should read corresponding property', async function () {
|
||||||
const html = await liquid.parseAndRender(`{{obj.name}}`, {obj: new CustomDrop()})
|
const html = await liquid.parseAndRender(`{{obj.name}}`, { obj: new CustomDrop() })
|
||||||
expect(html).to.equal('NAME')
|
expect(html).to.equal('NAME')
|
||||||
})
|
})
|
||||||
it('should output empty string if not exist', async function () {
|
it('should output empty string if not exist', async function () {
|
||||||
const html = await liquid.parseAndRender(`{{obj.foo}}`, {obj: new CustomDrop()})
|
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new CustomDrop() })
|
||||||
expect(html).to.equal('')
|
expect(html).to.equal('')
|
||||||
})
|
})
|
||||||
it('should respect liquidMethodMissing', async function () {
|
it('should respect liquidMethodMissing', async function () {
|
||||||
const html = await liquid.parseAndRender(`{{obj.foo}}`, {obj: new CustomDropWithMethodMissing()})
|
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new CustomDropWithMethodMissing() })
|
||||||
expect(html).to.equal('FOO')
|
expect(html).to.equal('FOO')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import Liquid from '../../../src/liquid'
|
import Liquid from '../../../src/liquid'
|
||||||
import * as chai from 'chai'
|
import * as chai from 'chai'
|
||||||
import { mock, restore } from '../../stub/mockfs'
|
import { mock, restore } from '../../stub/mockfs'
|
||||||
|
import * as chaiAsPromised from 'chai-as-promised'
|
||||||
|
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
|
chai.use(chaiAsPromised)
|
||||||
|
|
||||||
describe('Liquid', function () {
|
describe('Liquid', function () {
|
||||||
describe('#plugin()', function () {
|
describe('#plugin()', function () {
|
||||||
@@ -34,6 +36,12 @@ describe('Liquid', function () {
|
|||||||
const html = await engine.parseAndRender(tpl)
|
const html = await engine.parseAndRender(tpl)
|
||||||
expect(html).to.equal('Welcome(to]Liquid')
|
expect(html).to.equal('Welcome(to]Liquid')
|
||||||
})
|
})
|
||||||
|
it('should support for-in with variable', async function () {
|
||||||
|
const src = '{% assign total = 3 | minus: 1 %}' +
|
||||||
|
'{% for i in (1..total) %}{{ i }}{% endfor %}'
|
||||||
|
const html = await engine.parseAndRender(src, {})
|
||||||
|
return expect(html).to.equal('12')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
describe('#express()', function () {
|
describe('#express()', function () {
|
||||||
const liquid = new Liquid({ root: '/root' })
|
const liquid = new Liquid({ root: '/root' })
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
import Liquid from '../../src/liquid'
|
import Liquid from '../../src/liquid'
|
||||||
import { expect } from 'chai'
|
import { expect } from 'chai'
|
||||||
|
|
||||||
const liquid = new Liquid()
|
export const liquid = new Liquid()
|
||||||
|
|
||||||
export const ctx = {
|
export const ctx = {
|
||||||
date: new Date(),
|
date: new Date(),
|
||||||
|
|||||||
@@ -81,9 +81,6 @@ describe('util/underscore', function () {
|
|||||||
it('should return a range of integers', function () {
|
it('should return a range of integers', function () {
|
||||||
expect(_.range(3, 5)).to.deep.equal([3, 4])
|
expect(_.range(3, 5)).to.deep.equal([3, 4])
|
||||||
})
|
})
|
||||||
it('should start from 0 if begin omitted', function () {
|
|
||||||
expect(_.range(3)).to.deep.equal([0, 1, 2])
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
describe('.isObject()', function () {
|
describe('.isObject()', function () {
|
||||||
it('should return true for function', function () {
|
it('should return true for function', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user