Comparing version 1.0.2 to 1.0.3
@@ -1,3 +0,27 @@ | ||
export * from './required.js'; | ||
export * from './unpartial.js'; | ||
export declare function required<T extends Record<any, any>, R extends Record<any, any> = T, S extends Record<any, any> = T & R>(source1: Partial<T>, source2: Partial<R> | undefined | null, source3?: Partial<S> | null): T & R & S; | ||
export declare function requiredDeep<T extends Record<any, any>, R extends Record<any, any> = T, S extends Record<any, any> = T & R>(source1: Partial<T>, source2: Partial<R> | undefined | null, source3?: Partial<S> | null): T & R & S; | ||
/** | ||
* Unpartial a partial type. | ||
* @type T Type of `base`. If not specified, it will be inferred from `base`. | ||
* @param base The base value to fill in needed property if not available in the `partial` value. | ||
* @param partial The partial value to be unpartialed. | ||
*/ | ||
export declare function unpartial<T extends Record<any, any>, R extends Record<any, any> = Partial<T>>(base: T, partial: R | undefined | null): { | ||
[P in keyof T]: P extends keyof R ? T[P] | Exclude<R[P], undefined> : T[P]; | ||
} & Pick<R, Exclude<keyof R, keyof T>>; | ||
/** | ||
* Unpartial a partial type with two base values. | ||
* This is useful when you are extending value from another package or code. | ||
* That means you have a `parent` value from the original code, | ||
* a `base` value where you add additional defaults or override existing one, | ||
* and `partial` value from input. | ||
* @deprecated please use composition instead: `unpartial(unpartial(a, b), c)` | ||
* @type R Type of `base`. This type will be used as the return type. | ||
* @param parent The default value from the original code. | ||
* @param base The extended default value of your code. | ||
* @param partial The input value. | ||
*/ | ||
export declare function unpartial<T extends Record<any, any>, R extends Record<any, any> = Partial<T>, S extends Record<any, any> = Partial<T & R>>(parent: T, base: R | undefined | null, partial: S | undefined | null): T & R & S; | ||
export declare function unpartialRecursively<T extends Record<string, any>>(base: T, partial: Record<string, any> | undefined): T; | ||
export declare function unpartialRecursively<T extends Record<string, any>, R extends Record<string, any> = Record<string, any>>(superBase: R, base: T | undefined, partial: Record<string, unknown> | undefined): T & R; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./required.js"), exports); | ||
__exportStar(require("./unpartial.js"), exports); | ||
exports.unpartialRecursively = exports.unpartial = exports.requiredDeep = exports.required = void 0; | ||
function required(source1, source2, source3) { | ||
return merge([source1, source2, source3], function (p, e) { | ||
return __assign(__assign({}, p), e); | ||
}); | ||
} | ||
exports.required = required; | ||
function requiredDeep(source1, source2, source3) { | ||
return merge([source1, source2, source3], function (p, e) { return deepmerge(p, e); }); | ||
} | ||
exports.requiredDeep = requiredDeep; | ||
function merge(entries, reducer) { | ||
if (entries[0] === undefined || entries[0] === null) | ||
return entries[0]; | ||
return entries.filter(function (x) { return !!x; }).reduce(reducer, {}); | ||
} | ||
function deepmerge(source1, source2) { | ||
if (typeof source1 !== 'object' || source1 === null) | ||
return source2 !== undefined ? source2 : source1; | ||
if (Array.isArray(source1)) { | ||
if (Array.isArray(source2)) | ||
return source2; | ||
if (source2 === undefined) | ||
return __spreadArray([], source1, true); | ||
return __spreadArray(__spreadArray([], source1, true), [source2], false); | ||
} | ||
return getAllKeys(source1).concat(getAllKeys(source2)).reduce(function (p, k) { | ||
// @ts-ignore | ||
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; | ||
} | ||
function unpartial(s1, s2, s3) { | ||
// defensive check for JS | ||
if (s1 === undefined || s1 === null) | ||
return s1; | ||
return [s1, s2, s3] | ||
.filter(notNil) | ||
.reduce(function (p, s) { return (__assign(__assign({}, p), trimNilProps(s))); }, {}); | ||
} | ||
exports.unpartial = unpartial; | ||
function trimNilProps(value) { | ||
return Object.keys(value).reduce(function (p, k) { | ||
if (notNil(value[k])) | ||
p[k] = value[k]; | ||
return p; | ||
}, {}); | ||
} | ||
function notNil(value) { | ||
return value !== null && value !== undefined; | ||
} | ||
function unpartialRecursively(arg1, arg2, arg3) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
return requiredDeep(arg1, arg2, arg3); | ||
} | ||
exports.unpartialRecursively = unpartialRecursively; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,27 @@ | ||
export * from './required.js'; | ||
export * from './unpartial.js'; | ||
export declare function required<T extends Record<any, any>, R extends Record<any, any> = T, S extends Record<any, any> = T & R>(source1: Partial<T>, source2: Partial<R> | undefined | null, source3?: Partial<S> | null): T & R & S; | ||
export declare function requiredDeep<T extends Record<any, any>, R extends Record<any, any> = T, S extends Record<any, any> = T & R>(source1: Partial<T>, source2: Partial<R> | undefined | null, source3?: Partial<S> | null): T & R & S; | ||
/** | ||
* Unpartial a partial type. | ||
* @type T Type of `base`. If not specified, it will be inferred from `base`. | ||
* @param base The base value to fill in needed property if not available in the `partial` value. | ||
* @param partial The partial value to be unpartialed. | ||
*/ | ||
export declare function unpartial<T extends Record<any, any>, R extends Record<any, any> = Partial<T>>(base: T, partial: R | undefined | null): { | ||
[P in keyof T]: P extends keyof R ? T[P] | Exclude<R[P], undefined> : T[P]; | ||
} & Pick<R, Exclude<keyof R, keyof T>>; | ||
/** | ||
* Unpartial a partial type with two base values. | ||
* This is useful when you are extending value from another package or code. | ||
* That means you have a `parent` value from the original code, | ||
* a `base` value where you add additional defaults or override existing one, | ||
* and `partial` value from input. | ||
* @deprecated please use composition instead: `unpartial(unpartial(a, b), c)` | ||
* @type R Type of `base`. This type will be used as the return type. | ||
* @param parent The default value from the original code. | ||
* @param base The extended default value of your code. | ||
* @param partial The input value. | ||
*/ | ||
export declare function unpartial<T extends Record<any, any>, R extends Record<any, any> = Partial<T>, S extends Record<any, any> = Partial<T & R>>(parent: T, base: R | undefined | null, partial: S | undefined | null): T & R & S; | ||
export declare function unpartialRecursively<T extends Record<string, any>>(base: T, partial: Record<string, any> | undefined): T; | ||
export declare function unpartialRecursively<T extends Record<string, any>, R extends Record<string, any> = Record<string, any>>(superBase: R, base: T | undefined, partial: Record<string, unknown> | undefined): T & R; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,3 +0,60 @@ | ||
export * from './required.js'; | ||
export * from './unpartial.js'; | ||
export function required(source1, source2, source3) { | ||
return merge([source1, source2, source3], (p, e) => { | ||
return { ...p, ...e }; | ||
}); | ||
} | ||
export function requiredDeep(source1, source2, source3) { | ||
return merge([source1, source2, source3], (p, e) => deepmerge(p, e)); | ||
} | ||
function merge(entries, reducer) { | ||
if (entries[0] === undefined || entries[0] === null) | ||
return entries[0]; | ||
return entries.filter(x => !!x).reduce(reducer, {}); | ||
} | ||
function deepmerge(source1, source2) { | ||
if (typeof source1 !== 'object' || source1 === null) | ||
return source2 !== undefined ? source2 : source1; | ||
if (Array.isArray(source1)) { | ||
if (Array.isArray(source2)) | ||
return source2; | ||
if (source2 === undefined) | ||
return [...source1]; | ||
return [...source1, source2]; | ||
} | ||
return getAllKeys(source1).concat(getAllKeys(source2)).reduce((p, k) => { | ||
// @ts-ignore | ||
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; | ||
} | ||
export function unpartial(s1, s2, s3) { | ||
// defensive check for JS | ||
if (s1 === undefined || s1 === null) | ||
return s1; | ||
return [s1, s2, s3] | ||
.filter(notNil) | ||
.reduce((p, s) => ({ ...p, ...trimNilProps(s) }), {}); | ||
} | ||
function trimNilProps(value) { | ||
return Object.keys(value).reduce((p, k) => { | ||
if (notNil(value[k])) | ||
p[k] = value[k]; | ||
return p; | ||
}, {}); | ||
} | ||
function notNil(value) { | ||
return value !== null && value !== undefined; | ||
} | ||
export function unpartialRecursively(arg1, arg2, arg3) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
return requiredDeep(arg1, arg2, arg3); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "unpartial", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Unpartial a partialed object", | ||
@@ -59,6 +59,6 @@ "homepage": "https://github.com/unional/unpartial", | ||
"ts-jest": "^28.0.8", | ||
"type-plus": "^4.11.1", | ||
"typescript": "^4.8.2" | ||
"type-plus": "^4.12.0", | ||
"typescript": "^4.8.3" | ||
}, | ||
"packageManager": "pnpm@7.9.5", | ||
"packageManager": "pnpm@7.11.0", | ||
"engines": { | ||
@@ -86,3 +86,6 @@ "node": ">=6" | ||
"lint": "eslint --ext=ts,js .", | ||
"nuke": "run-s clean nuke:local", | ||
"nuke:local": "rimraf node_modules || true", | ||
"_postinstall": "husky install", | ||
"release": "pnpm cs publish", | ||
"size": "size-limit", | ||
@@ -89,0 +92,0 @@ "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest", |
@@ -54,2 +54,11 @@ # unpartial | ||
**This is deprecated** because currently TypeScript does not support optional generic type, | ||
so it is not possible to create a satisfactory signature that works with both implicit and explicit type. | ||
Instead, please use composition when combining 3 or more values: | ||
```ts | ||
unpartial(unpartial(defaultOption, myDefaultOption), givenOption) | ||
``` | ||
There are 3 more functions available in this library: | ||
@@ -60,5 +69,5 @@ | ||
- `required()`: an alternative version of `unpartial()` with a different type management.\ | ||
This will be deprecated in the future. | ||
This will become identical to `unpartial()` in the future. | ||
- `requiredDeep()`: an alternative version of `unpartialRecursively()` with a different type management.\ | ||
This will be deprecated in the future. | ||
This will become identical to `unpartial()` in the future. | ||
@@ -65,0 +74,0 @@ `unpartial` is also exposed through [`type-plus`](https://github.com/unional/type-plus). |
116
ts/index.ts
@@ -1,2 +0,114 @@ | ||
export * from './required.js' | ||
export * from './unpartial.js' | ||
export function required< | ||
T extends Record<any, any>, | ||
R extends Record<any, any> = T, | ||
S extends Record<any, any> = T & R | ||
>(source1: Partial<T>, source2: Partial<R> | undefined | null, source3?: Partial<S> | null): T & R & S { | ||
return merge([source1, source2, source3], (p, e) => { | ||
return { ...p, ...e } | ||
}) | ||
} | ||
export function requiredDeep< | ||
T extends Record<any, any>, | ||
R extends Record<any, any> = T, | ||
S extends Record<any, any> = T & R | ||
>(source1: Partial<T>, source2: Partial<R> | undefined | null, source3?: Partial<S> | null): T & R & S { | ||
return merge([source1, source2, source3], (p, e) => deepmerge(p, e) as any) | ||
} | ||
function merge( | ||
entries: [Record<any, any>, Record<any, any> | undefined | null, Record<any, any> | undefined | null], | ||
reducer: (result: Record<any, any>, entry: Record<any, any> | undefined | null) => Record<any, any> | ||
) { | ||
if (entries[0] === undefined || entries[0] === null) return entries[0] | ||
return entries.filter(x => !!x).reduce(reducer, {}) | ||
} | ||
function deepmerge( | ||
source1: unknown, | ||
source2: unknown | ||
): unknown { | ||
if (typeof source1 !== 'object' || source1 === null) return source2 !== undefined ? source2 : source1 | ||
if (Array.isArray(source1)) { | ||
if (Array.isArray(source2)) return source2 | ||
if (source2 === undefined) return [...source1] | ||
return [...source1, source2] | ||
} | ||
return getAllKeys(source1).concat(getAllKeys(source2)).reduce((p, k) => { | ||
// @ts-ignore | ||
p[k] = deepmerge(source1[k], source2 && source2[k]) | ||
return p | ||
}, {} as Record<keyof any, unknown>) | ||
} | ||
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 | ||
} | ||
/** | ||
* Unpartial a partial type. | ||
* @type T Type of `base`. If not specified, it will be inferred from `base`. | ||
* @param base The base value to fill in needed property if not available in the `partial` value. | ||
* @param partial The partial value to be unpartialed. | ||
*/ | ||
export function unpartial< | ||
T extends Record<any, any>, | ||
R extends Record<any, any> = Partial<T> | ||
>(base: T, partial: R | undefined | null): | ||
{ [P in keyof T]: P extends keyof R ? T[P] | Exclude<R[P], undefined> : T[P] } | ||
& Pick<R, Exclude<keyof R, keyof T>> | ||
/** | ||
* Unpartial a partial type with two base values. | ||
* This is useful when you are extending value from another package or code. | ||
* That means you have a `parent` value from the original code, | ||
* a `base` value where you add additional defaults or override existing one, | ||
* and `partial` value from input. | ||
* @deprecated please use composition instead: `unpartial(unpartial(a, b), c)` | ||
* @type R Type of `base`. This type will be used as the return type. | ||
* @param parent The default value from the original code. | ||
* @param base The extended default value of your code. | ||
* @param partial The input value. | ||
*/ | ||
export function unpartial< | ||
T extends Record<any, any>, | ||
R extends Record<any, any> = Partial<T>, | ||
S extends Record<any, any> = Partial<T & R> | ||
>(parent: T, base: R | undefined | null, partial: S | undefined | null): T & R & S | ||
export function unpartial(s1: any, s2: any, s3?: any) { | ||
// defensive check for JS | ||
if (s1 === undefined || s1 === null) return s1 | ||
return [s1, s2, s3] | ||
.filter(notNil) | ||
.reduce((p, s: any) => ({ ...p, ...trimNilProps(s) }), {} as any) | ||
} | ||
function trimNilProps(value: any) { | ||
return Object.keys(value).reduce((p, k) => { | ||
if (notNil(value[k])) p[k] = value[k] | ||
return p | ||
}, {} as any) | ||
} | ||
function notNil(value: any) { | ||
return value !== null && value !== undefined | ||
} | ||
export function unpartialRecursively< | ||
T extends Record<string, any> | ||
>(base: T, partial: Record<string, any> | undefined): T | ||
export function unpartialRecursively< | ||
T extends Record<string, any>, | ||
R extends Record<string, any> = Record<string, any> | ||
>(superBase: R, base: T | undefined, partial: Record<string, unknown> | undefined): T & R | ||
export function unpartialRecursively(arg1: any, arg2: any, arg3?: any) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
return requiredDeep(arg1, arg2, arg3) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104
31066
13
300
1