Skip to content

Commit

Permalink
remove useless hasOwnProperty calls, closes #423
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 24, 2020
1 parent c8a6c15 commit dc25290
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 2.1.2

- **Polish**
- remove useless `hasOwnProperty` calls, closes #423 (@gcanti)

# 2.1.1

- **Bug Fix**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-ts",
"version": "2.1.1",
"version": "2.1.2",
"description": "TypeScript compatible runtime type system for IO validation",
"files": [
"lib",
Expand Down
10 changes: 2 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ export const type = <P extends Props>(props: P, name: string = getInterfaceTypeN
if (UnknownRecord.is(u)) {
for (let i = 0; i < len; i++) {
const k = keys[i]
if (!hasOwnProperty.call(u, k) || !types[i].is(u[k])) {
if (!types[i].is(u[k])) {
return false
}
}
Expand All @@ -812,20 +812,14 @@ export const type = <P extends Props>(props: P, name: string = getInterfaceTypeN
const errors: Errors = []
for (let i = 0; i < len; i++) {
const k = keys[i]
if (!hasOwnProperty.call(a, k)) {
if (a === o) {
a = { ...o }
}
a[k] = a[k]
}
const ak = a[k]
const type = types[i]
const result = type.validate(ak, appendContext(c, k, type, ak))
if (isLeft(result)) {
pushAll(errors, result.left)
} else {
const vak = result.right
if (vak !== ak) {
if (vak !== ak || (vak === undefined && !hasOwnProperty.call(a, k))) {
/* istanbul ignore next */
if (a === o) {
a = { ...o }
Expand Down
26 changes: 26 additions & 0 deletions test/strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ describe('strict', () => {
assert.strictEqual(T.is({ a: 1, b: 1 }), true)
assert.strictEqual(T.is(undefined), false)
})

it('#423', () => {
class A {
get a() {
return 'a'
}
get b() {
return 'b'
}
}
const T = t.strict({ a: t.string, b: t.string })
assert.strictEqual(T.is(new A()), true)
})
})

describe('decode', () => {
Expand Down Expand Up @@ -57,6 +70,19 @@ describe('strict', () => {
const T = t.strict({ foo: t.string })
assertSuccess(T.decode({ foo: 'foo', bar: 1, baz: true }), { foo: 'foo' })
})

it('#423', () => {
class A {
get a() {
return 'a'
}
get b() {
return 'b'
}
}
const T = t.strict({ a: t.string, b: t.string })
assertSuccess(T.decode(new A()))
})
})

describe('encode', () => {
Expand Down
26 changes: 26 additions & 0 deletions test/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ describe('type', () => {
const T = t.type({ a: t.string })
assert.strictEqual(T.is({ a: 'a', b: 1 }), true)
})

it('#423', () => {
class A {
get a() {
return 'a'
}
get b() {
return 'b'
}
}
const T = t.type({ a: t.string, b: t.string })
assert.strictEqual(T.is(new A()), true)
})
})

describe('decode', () => {
Expand Down Expand Up @@ -75,6 +88,19 @@ describe('type', () => {
const T = t.interface({ a: t.string })
assertSuccess(T.decode({ a: 'a' }))
})

it('#423', () => {
class A {
get a() {
return 'a'
}
get b() {
return 'b'
}
}
const T = t.type({ a: t.string, b: t.string })
assertSuccess(T.decode(new A()))
})
})

describe('encode', () => {
Expand Down

0 comments on commit dc25290

Please sign in to comment.