Skip to content

Commit fdb4f5c

Browse files
committed
throw NaN which I know is weird. I can revert this later on
1 parent bcae129 commit fdb4f5c

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

compatible.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('All of the compatible tests', () => {
5656
if (Array.isArray(result)) result = result.map(i => (i || 0).toNumber ? Number(i) : i)
5757
expect(correction(result)).toStrictEqual(testCase.result)
5858
} catch (err) {
59-
if (err.message.includes('expect')) throw err
59+
if (err.message && err.message.includes('expect')) throw err
6060
expect(testCase.error).toStrictEqual(true)
6161
}
6262
})
@@ -71,7 +71,7 @@ describe('All of the compatible tests', () => {
7171
if (Array.isArray(result)) result = result.map(i => i.toNumber ? Number(i) : i)
7272
expect(correction(result)).toStrictEqual(testCase.result)
7373
} catch (err) {
74-
if (err.message.includes('expect')) throw err
74+
if (err.message && err.message.includes('expect')) throw err
7575
expect(testCase.error).toStrictEqual(true)
7676
}
7777
})

defaultMethods.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,58 +59,58 @@ const defaultMethods = {
5959
if (typeof data === 'string') return precoerceNumber(+data)
6060
if (typeof data === 'number') return precoerceNumber(+data)
6161
if (typeof data === 'boolean') return precoerceNumber(+data)
62-
if (typeof data === 'object' && !Array.isArray(data)) throw new Error('NaN')
62+
if (typeof data === 'object' && !Array.isArray(data)) throw NaN
6363
let res = 0
6464
for (let i = 0; i < data.length; i++) {
65-
if (data[i] && typeof data[i] === 'object') throw new Error('NaN')
65+
if (data[i] && typeof data[i] === 'object') throw NaN
6666
res += +data[i]
6767
}
68-
if (Number.isNaN(res)) throw new Error('NaN')
68+
if (Number.isNaN(res)) throw NaN
6969
return res
7070
},
7171
'*': (data) => {
7272
let res = 1
7373
for (let i = 0; i < data.length; i++) {
74-
if (data[i] && typeof data[i] === 'object') throw new Error('NaN')
74+
if (data[i] && typeof data[i] === 'object') throw NaN
7575
res *= +data[i]
7676
}
77-
if (Number.isNaN(res)) throw new Error('NaN')
77+
if (Number.isNaN(res)) throw NaN
7878
return res
7979
},
8080
'/': (data) => {
81-
if (data[0] && typeof data[0] === 'object') throw new Error('NaN')
81+
if (data[0] && typeof data[0] === 'object') throw NaN
8282
let res = +data[0]
8383
for (let i = 1; i < data.length; i++) {
84-
if ((data[i] && typeof data[i] === 'object') || !data[i]) throw new Error('NaN')
84+
if ((data[i] && typeof data[i] === 'object') || !data[i]) throw NaN
8585
res /= +data[i]
8686
}
87-
if (Number.isNaN(res)) throw new Error('NaN')
87+
if (Number.isNaN(res)) throw NaN
8888
return res
8989
},
9090
'-': (data) => {
9191
if (!data) return 0
9292
if (typeof data === 'string') return precoerceNumber(-data)
9393
if (typeof data === 'number') return precoerceNumber(-data)
9494
if (typeof data === 'boolean') return precoerceNumber(-data)
95-
if (typeof data === 'object' && !Array.isArray(data)) throw new Error('NaN')
96-
if (data[0] && typeof data[0] === 'object') throw new Error('NaN')
95+
if (typeof data === 'object' && !Array.isArray(data)) throw NaN
96+
if (data[0] && typeof data[0] === 'object') throw NaN
9797
if (data.length === 1) return -data[0]
9898
let res = data[0]
9999
for (let i = 1; i < data.length; i++) {
100-
if (data[i] && typeof data[i] === 'object') throw new Error('NaN')
100+
if (data[i] && typeof data[i] === 'object') throw NaN
101101
res -= +data[i]
102102
}
103-
if (Number.isNaN(res)) throw new Error('NaN')
103+
if (Number.isNaN(res)) throw NaN
104104
return res
105105
},
106106
'%': (data) => {
107-
if (data[0] && typeof data[0] === 'object') throw new Error('NaN')
107+
if (data[0] && typeof data[0] === 'object') throw NaN
108108
let res = +data[0]
109109
for (let i = 1; i < data.length; i++) {
110-
if (data[i] && typeof data[i] === 'object') throw new Error('NaN')
110+
if (data[i] && typeof data[i] === 'object') throw NaN
111111
res %= +data[i]
112112
}
113-
if (Number.isNaN(res)) throw new Error('NaN')
113+
if (Number.isNaN(res)) throw NaN
114114
return res
115115
},
116116
error: (type) => {
@@ -347,8 +347,8 @@ const defaultMethods = {
347347
else item = executeInLoop ? engine.run(arr[i], _1, { above: _2 }) : arr[i]
348348
return item
349349
} catch (e) {
350-
// Do nothing
351-
lastError = e
350+
if (Number.isNaN(e)) lastError = { message: 'NaN' }
351+
else lastError = e
352352
}
353353
}
354354

@@ -368,8 +368,8 @@ const defaultMethods = {
368368
else item = executeInLoop ? await engine.run(arr[i], _1, { above: _2 }) : arr[i]
369369
return item
370370
} catch (e) {
371-
// Do nothing
372-
lastError = e
371+
if (Number.isNaN(e)) lastError = { message: 'NaN' }
372+
else lastError = e
373373
}
374374
}
375375

@@ -962,11 +962,11 @@ defaultMethods['/'].compile = function (data, buildState) {
962962
if (Array.isArray(data)) {
963963
return `(${data.map((i, x) => {
964964
let res = numberCoercion(i, buildState)
965-
if (x) res = `(${res}|| (() => { throw new Error('NaN') })() )`
965+
if (x) res = `(${res}|| (() => { throw NaN })() )`
966966
return res
967967
}).join(' / ')})`
968968
}
969-
return `(${buildString(data, buildState)}).reduce((a,b) => (+precoerceNumber(a))/(+precoerceNumber(b) || (() => { throw new Error('NaN') })() ))`
969+
return `(${buildString(data, buildState)}).reduce((a,b) => (+precoerceNumber(a))/(+precoerceNumber(b) || (() => { throw NaN })() ))`
970970
}
971971
// @ts-ignore Allow custom attribute
972972
defaultMethods['*'].compile = function (data, buildState) {

utilities/downgrade.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* @param {any} item
44
*/
55
export function precoerceNumber (item) {
6-
if (Number.isNaN(item)) throw new Error('NaN')
6+
if (Number.isNaN(item)) throw NaN
77
if (!item) return item
8-
if (typeof item === 'object') throw new Error('NaN')
8+
if (typeof item === 'object') throw NaN
99
return item
1010
}

0 commit comments

Comments
 (0)