Skip to content

Commit

Permalink
improve internal mergeAll function, closes #532
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Nov 20, 2020
1 parent 1de671b commit 2881d94
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 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.2.13

- **Bug Fix**
- improve internal `mergeAll` function, closes #532 (@gcanti)

# 2.2.12

- **Experimental**
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.2.12",
"version": "2.2.13",
"description": "TypeScript runtime type system for IO decoding/encoding",
"main": "lib/index.js",
"module": "es6/index.js",
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1416,9 +1416,13 @@ export interface IntersectionC<CS extends [Mixed, Mixed, ...Array<Mixed>]>
unknown
> {}

const mergeAll = (base: any, us: Array<any>): any => {
/**
* @internal
*/
export const mergeAll = (base: any, us: Array<any>): any => {
let equal = true
let primitive = true
const baseIsNotADictionary = !UnknownRecord.is(base)
for (const u of us) {
if (u !== base) {
equal = false
Expand All @@ -1435,7 +1439,7 @@ const mergeAll = (base: any, us: Array<any>): any => {
let r: any = {}
for (const u of us) {
for (const k in u) {
if (u[k] !== base[k] || !r.hasOwnProperty(k)) {
if (!r.hasOwnProperty(k) || baseIsNotADictionary || u[k] !== base[k]) {
r[k] = u[k]
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/2.1.x/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import * as t from '../../src/index'
import { assertFailure, assertStrictEqual, assertSuccess, NumberFromString } from './helpers'

describe('intersection', () => {
it('mergeAll', () => {
assert.deepStrictEqual(t.mergeAll(undefined, [{ prop1: 'b', prop2: 2 }, { prop1: 'a' }, { prop2: 1 }]), {
prop1: 'a',
prop2: 1
})
})

describe('name', () => {
it('should assign a default name', () => {
const T = t.intersection([t.type({ a: t.string }), t.type({ b: t.number })])
Expand Down

0 comments on commit 2881d94

Please sign in to comment.