Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

unpartial

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unpartial - npm Package Compare versions

Comparing version 0.6.0 to 0.6.1

26

lib/required.js

@@ -13,7 +13,3 @@ "use strict";

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var deepmerge_1 = __importDefault(require("deepmerge"));
function required(source1, source2, source3) {

@@ -24,3 +20,3 @@ return merge([source1, source2, source3], function (p, e) { return (__assign({}, p, e)); });

function requiredDeep(source1, source2, source3) {
return merge([source1, source2, source3], function (p, e) { return deepmerge_1.default(p, e); });
return merge([source1, source2, source3], function (p, e) { return deepmerge(p, e); });
}

@@ -33,2 +29,22 @@ exports.requiredDeep = requiredDeep;

}
function deepmerge(source1, source2) {
if (typeof source1 !== 'object' || source1 === null)
return source2 || source1;
if (Array.isArray(source1)) {
return Array.isArray(source2) ? source2 : source1.concat([source2]);
}
return getAllKeys(source1).concat(getAllKeys(source2)).reduce(function (p, k) {
p[k] = deepmerge(source1[k], source2 && source2[k]);
return p;
}, {});
}
function getAllKeys(subject, internal) {
if (internal === void 0) { internal = false; }
if (typeof subject !== 'object')
return [];
var propertyNames = Object.getOwnPropertyNames(subject);
var keys = internal ? propertyNames.filter(function (n) { return n !== 'constructor'; }) : propertyNames;
var proto = Object.getPrototypeOf(subject);
return proto !== Object.prototype ? keys.concat(getAllKeys(proto, true)) : keys;
}
//# sourceMappingURL=required.js.map

@@ -1,2 +0,1 @@

import deepmerge from 'deepmerge';
export function required(source1, source2, source3) {

@@ -13,2 +12,21 @@ return merge([source1, source2, source3], (p, e) => ({ ...p, ...e }));

}
function deepmerge(source1, source2) {
if (typeof source1 !== 'object' || source1 === null)
return source2 || source1;
if (Array.isArray(source1)) {
return Array.isArray(source2) ? source2 : [...source1, source2];
}
return getAllKeys(source1).concat(getAllKeys(source2)).reduce((p, k) => {
p[k] = deepmerge(source1[k], source2 && source2[k]);
return p;
}, {});
}
function getAllKeys(subject, internal = false) {
if (typeof subject !== 'object')
return [];
const propertyNames = Object.getOwnPropertyNames(subject);
const keys = internal ? propertyNames.filter(n => n !== 'constructor') : propertyNames;
const proto = Object.getPrototypeOf(subject);
return proto !== Object.prototype ? keys.concat(getAllKeys(proto, true)) : keys;
}
//# sourceMappingURL=required.js.map

20

package.json
{
"name": "unpartial",
"version": "0.6.0",
"version": "0.6.1",
"description": "Unpartial a partialed object",

@@ -39,12 +39,5 @@ "homepage": "https://github.com/unional/unpartial",

},
"size-limit": [
{
"limit": "5 KB"
}
],
"dependencies": {
"deepmerge": "^3.2.0"
},
"dependencies": {},
"devDependencies": {
"@unional/devpkg-node": "^1.2.5",
"@unional/devpkg-node": "^1.3.0",
"assertron": "^7.0.1",

@@ -55,3 +48,8 @@ "size-limit": "^1.3.1"

"node": ">=6"
}
},
"size-limit": [
{
"limit": "5 KB"
}
]
}

@@ -1,102 +0,140 @@

import { required } from './required';
import { required, requiredDeep } from './required';
test('none of the inputs are modified', () => {
const source1 = { a: 1 }
const source2 = { b: 2 }
const source3 = { c: 3 }
required(source1, source2, source3)
describe('required()', () => {
test('none of the inputs are modified', () => {
const source1 = { a: 1 }
const source2 = { b: 2 }
const source3 = { c: 3 }
required(source1, source2, source3)
expect(source1).toEqual({ a: 1 })
expect(source2).toEqual({ b: 2 })
expect(source3).toEqual({ c: 3 })
})
expect(source1).toEqual({ a: 1 })
expect(source2).toEqual({ b: 2 })
expect(source3).toEqual({ c: 3 })
})
test('source2 can be undefined', () => {
const actual = required({ a: 1 }, undefined)
test('source2 can be undefined', () => {
const actual = required({ a: 1 }, undefined)
expect(actual).toEqual({ a: 1 })
})
expect(actual).toEqual({ a: 1 })
})
test('source2 can be null', () => {
const actual = required({ a: 1 }, null)
test('source2 can be null', () => {
const actual = required({ a: 1 }, null)
expect(actual).toEqual({ a: 1 })
})
expect(actual).toEqual({ a: 1 })
})
test('source3 can be undefined', () => {
const actual = required({ a: 1 }, undefined, undefined)
test('source3 can be undefined', () => {
const actual = required({ a: 1 }, undefined, undefined)
expect(actual).toEqual({ a: 1 })
})
expect(actual).toEqual({ a: 1 })
})
test('source3 can be null', () => {
const actual = required({ a: 1 }, null, null)
test('source3 can be null', () => {
const actual = required({ a: 1 }, null, null)
expect(actual).toEqual({ a: 1 })
})
expect(actual).toEqual({ a: 1 })
})
type Source1 = {
a: string,
b: number,
c: { d: boolean }
e?: string
};
type Source1 = {
a: string,
b: number,
c: { d: boolean }
e?: string
};
test('can access types in source1', () => {
const source1: Partial<Source1> = { a: 'a', b: 2, c: { d: true } }
test('can access types in source1', () => {
const source1: Partial<Source1> = { a: 'a', b: 2, c: { d: true } }
const actual = required(source1, undefined)
const actual = required(source1, undefined)
expect(actual.a).toEqual('a')
expect(actual.b).toEqual(2)
expect(actual.c.d).toEqual(true)
actual.e = undefined
})
expect(actual.a).toEqual('a')
expect(actual.b).toEqual(2)
expect(actual.c.d).toEqual(true)
actual.e = undefined
})
type Source2 = {
p: string,
q: number,
r: { s: boolean }
t?: string
};
type Source2 = {
p: string,
q: number,
r: { s: boolean }
t?: string
};
test('can access types in source2', () => {
const source1: Partial<Source1> = { a: 'a', b: 2, c: { d: true } }
const source2: Partial<Source2> = { p: 'p', q: 2, r: { s: true } }
test('can access types in source2', () => {
const source1: Partial<Source1> = { a: 'a', b: 2, c: { d: true } }
const source2: Partial<Source2> = { p: 'p', q: 2, r: { s: true } }
const actual = required(source1, source2)
const actual = required(source1, source2)
expect(actual.p).toEqual('p')
expect(actual.q).toEqual(2)
expect(actual.r.s).toEqual(true)
actual.t = undefined
})
expect(actual.p).toEqual('p')
expect(actual.q).toEqual(2)
expect(actual.r.s).toEqual(true)
actual.t = undefined
})
type Source3 = {
w: string,
x: number,
y: { z: boolean }
u?: string
}
type Source3 = {
w: string,
x: number,
y: { z: boolean }
u?: string
};
test('can access types in source3', () => {
const source1: Partial<Source1> = { a: 'a', b: 2, c: { d: true } }
const source2: Partial<Source2> = { p: 'p', q: 2, r: { s: true } }
const source3: Partial<Source3> = { w: 'w', x: 2, y: { z: true } }
test('can access types in source3', () => {
const source1: Partial<Source1> = { a: 'a', b: 2, c: { d: true } }
const source2: Partial<Source2> = { p: 'p', q: 2, r: { s: true } }
const source3: Partial<Source3> = { w: 'w', x: 2, y: { z: true } }
const actual = required(source1, source2, source3)
const actual = required(source1, source2, source3)
expect(actual.w).toEqual('w')
expect(actual.x).toEqual(2)
expect(actual.y.z).toEqual(true)
actual.u = undefined
expect(actual.w).toEqual('w')
expect(actual.x).toEqual(2)
expect(actual.y.z).toEqual(true)
actual.u = undefined
})
test('can explicitly specify target type', () => {
const actual = required<Source1>({ a: 'a' }, { b: 2 }, { c: { d: true } })
expect(actual.a).toEqual('a')
expect(actual.b).toEqual(2)
expect(actual.c.d).toEqual(true)
actual.e = undefined
})
})
test('can explicitly specify target type', () => {
const actual = required<Source1>({ a: 'a' }, { b: 2 }, { c: { d: true } })
describe('requiredDeep()', () => {
test('merge class instance', () => {
class Foo {
a = 1
c() {
return this.a
}
}
expect(actual.a).toEqual('a')
expect(actual.b).toEqual(2)
expect(actual.c.d).toEqual(true)
actual.e = undefined
class Boo extends Foo {
d() { return this.a + 1 }
}
const boo = new Boo()
const actual = requiredDeep({ a: { b: { x: 1 } } }, undefined, { a: { b: boo } })
expect(actual).toEqual({
a: {
b: {
a: 1,
x: 1,
c: boo.c,
d: boo.d
}
}
})
})
test('override array', () => {
const actual = requiredDeep({ a: [1] }, { a: [2] })
expect(actual).toEqual({ a: [2] })
})
test('value add to array', () => {
expect(requiredDeep({ a: [1] }, { a: 2 })).toEqual({ a: [1, 2] })
})
})

@@ -1,3 +0,1 @@

import deepmerge from 'deepmerge'
export function required<

@@ -24,1 +22,22 @@ T extends Record<any, any>,

}
function deepmerge(source1: any, source2: any): any {
if (typeof source1 !== 'object' || source1 === null) return source2 || source1
if (Array.isArray(source1)) {
return Array.isArray(source2) ? source2 : [...source1, source2]
}
return getAllKeys(source1).concat(getAllKeys(source2)).reduce((p, k) => {
p[k] = deepmerge(source1[k], source2 && source2[k])
return p
}, {} as any)
}
function getAllKeys(subject: any, internal = false): string[] {
if (typeof subject !== 'object') return []
const propertyNames = Object.getOwnPropertyNames(subject)
const keys = internal ? propertyNames.filter(n => n !== 'constructor') : propertyNames
const proto = Object.getPrototypeOf(subject)
return proto !== Object.prototype ? keys.concat(getAllKeys(proto, true)) : keys
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc