From 320ae5aac72fb56b0e188b3df14069d7789097c5 Mon Sep 17 00:00:00 2001 From: gcanti Date: Mon, 20 May 2019 11:45:45 +0200 Subject: [PATCH] handle null prototype objects --- src/Record.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Record.ts b/src/Record.ts index 37fa4e1f7..4c2d7f888 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -12,6 +12,12 @@ import { fromEquals, Setoid } from './Setoid' import { Unfoldable, Unfoldable1 } from './Unfoldable' import { Show, showString } from './Show' +const _hasOwnProperty = Object.prototype.hasOwnProperty + +function hasOwnProperty(k: K, d: Record): boolean { + return _hasOwnProperty.call(d, k) +} + /** * @since 1.17.0 */ @@ -118,7 +124,7 @@ export function remove( ): Record, A> export function remove(k: string, d: Record): Record export function remove(k: string, d: Record): Record { - if (!d.hasOwnProperty(k)) { + if (!hasOwnProperty(k, d)) { return d } const r = Object.assign({}, d) @@ -143,7 +149,7 @@ export const pop = (k: string, d: Record): Option<[A, Record(S: Setoid) => (d1: Record, d2: Record): boolean => { for (let k in d1) { - if (!d2.hasOwnProperty(k) || !S.equals(d1[k], d2[k])) { + if (!hasOwnProperty(k, d2) || !S.equals(d1[k], d2[k])) { return false } } @@ -193,7 +199,7 @@ export function getMonoid(S: Semigroup): Monoid> { * @since 1.10.0 */ export const lookup = (key: string, fa: Record): Option => { - return fa.hasOwnProperty(key) ? optionSome(fa[key]) : none + return hasOwnProperty(key, fa) ? optionSome(fa[key]) : none } /** @@ -645,7 +651,7 @@ export function filterWithKey(fa: Record, p: (key: string, a: A) = const r: Record = {} let changed = false for (const key in fa) { - if (fa.hasOwnProperty(key)) { + if (hasOwnProperty(key, fa)) { const a = fa[key] if (p(key, a)) { r[key] = a @@ -682,7 +688,7 @@ export function fromFoldable( ): (ta: HKT, onConflict: (existing: A, a: A) => A) => Record { return (ta: HKT, f: (existing: A, a: A) => A) => { return F.reduce<[string, A], Record>(ta, {}, (b, [k, a]) => { - b[k] = b.hasOwnProperty(k) ? f(b[k], a) : a + b[k] = hasOwnProperty(k, b) ? f(b[k], a) : a return b }) } @@ -750,7 +756,7 @@ export function fromFoldableMap( return (ta: HKT, f: (a: A) => [string, B]) => { return F.reduce>(ta, {}, (r, a) => { const [k, b] = f(a) - r[k] = r.hasOwnProperty(k) ? M.concat(r[k], b) : b + r[k] = hasOwnProperty(k, r) ? M.concat(r[k], b) : b return r }) }