Skip to content

Commit

Permalink
fix(hasOne): Issues with eval
Browse files Browse the repository at this point in the history
add(polyfills): Array includes polyfill
  • Loading branch information
danibram committed May 8, 2018
1 parent 06f0b7b commit f1ab9e8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
55 changes: 55 additions & 0 deletions src/array-includes.ts
@@ -0,0 +1,55 @@
if (!(Array.prototype as any).includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(searchElement, fromIndex) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined')
}

const o = Object(this)

// 2. Let len be ? ToLength(? Get(O, "length")).
const len = o.length >>> 0

// 3. If len is 0, return false.
if (len === 0) {
return false
}

// 4. Let n be ? ToInteger(fromIndex).
// (If fromIndex is undefined, this step produces the value 0.)
const n = fromIndex | 0

// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
let k = Math.max(n >= 0 ? n : len - Math.abs(n), 0)

function sameValueZero(x, y) {
return (
x === y ||
(typeof x === 'number' &&
typeof y === 'number' &&
isNaN(x) &&
isNaN(y))
)
}

// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
// c. Increase k by 1.
if (sameValueZero(o[k], searchElement)) {
return true
}
k++
}

// 8. Return false
return false
}
})
}
2 changes: 2 additions & 0 deletions src/index.ts
@@ -1,3 +1,5 @@
import './array-includes'

import { Mocker } from './lib/Mocker'
export * from './lib/Mocker'
export * from './lib/Schema'
Expand Down
14 changes: 13 additions & 1 deletion src/lib/tests/Generator.hasOne.spec.ts
Expand Up @@ -97,7 +97,19 @@ test('Should get one of the DB', async t => {
t.true(set1.indexOf(res) > -1)
})

test('Should get one of the DB, and one field of that entity', async t => {
test('Should get one of the DB, and one field of that entity (eval)', async t => {
gen.DB = {
hello: set1
}

let res = gen.hasOne({ hasOne: 'hello', get: 'id', eval: true })
t.true(res !== undefined)
t.true(res !== null)
t.true(res <= 10)
t.true(res >= 0)
})

test('Should get one of the DB, and one field of that entity (no-eval)', async t => {
gen.DB = {
hello: set1
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/utils.ts
Expand Up @@ -153,7 +153,9 @@ export const fnParser = function(name, fn, cfg) {
let [args2, mods] = args.split(')')

args = args2
? args2[0] === '{' ? [JSON.parse(args2)] : args2.split(',')
? args2[0] === '{'
? [JSON.parse(args2)]
: args2.split(',')
: []

let result = func.call(this, ...args)
Expand All @@ -179,7 +181,7 @@ export const fnParser = function(name, fn, cfg) {
export const loopInside = function(object: any, path: string) {
let p = path.split('.')
return p.reduce((acc, val) => {
if (!acc[val]) {
if (acc[val] === null) {
throw `'${acc}' doesnt have key '${val}'.`
}
return acc[val]
Expand Down

0 comments on commit f1ab9e8

Please sign in to comment.