@ts-common/property-set
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -1,3 +0,3 @@ | ||
export declare type MapFunc<T, R> = <K extends keyof T>(k: K, v: T[K]) => R; | ||
export declare function forEach<T>(source: T, func: MapFunc<T, void>): void; | ||
export declare type MapFunc<T, R> = <K extends keyof T>(v: T[K], k: K) => R; | ||
export declare const forEach: <T>(source: T, func: MapFunc<T, void>) => void; | ||
export declare type PropertyFactory<T, K extends keyof T> = (k: K) => T[K]; | ||
@@ -7,10 +7,10 @@ export declare type Factory<T> = { | ||
}; | ||
export declare function copyProperty<T>(value: T): <K extends keyof T>(k: K) => T[K]; | ||
export declare const copyProperty: <T>(value: T) => <K extends keyof T>(k: K) => T[K]; | ||
export declare type MutableOptional<T> = { | ||
[K in keyof T]?: T[K]; | ||
}; | ||
export declare function create<T>(factory: Factory<T>): T; | ||
export declare const create: <T>(factory: Factory<T>) => T; | ||
export declare type PartialFactory<T> = { | ||
readonly [K in keyof T]?: (k: K, v: T[K]) => T[K]; | ||
readonly [K in keyof T]?: (v: T[K], k: K) => T[K]; | ||
}; | ||
export declare function copyCreate<T>(source: T, factory: PartialFactory<T>): T; | ||
export declare const copyCreate: <T>(source: T, factory: PartialFactory<T>) => T; |
44
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function forEach(source, func) { | ||
exports.forEach = (source, func) => { | ||
// tslint:disable-next-line:forin | ||
for (const key in source) { | ||
func(key, source[key]); | ||
func(source[key], key); | ||
} | ||
} | ||
exports.forEach = forEach; | ||
function copyProperty(value) { | ||
return (k) => value[k]; | ||
} | ||
exports.copyProperty = copyProperty; | ||
function fromMutableOptional(v) { | ||
return v; | ||
} | ||
function setProperty(result, k, v) { | ||
}; | ||
exports.copyProperty = (value) => (k) => value[k]; | ||
const fromMutableOptional = (v) => v; | ||
const setProperty = (result, k, v) => { | ||
if (v !== undefined) { | ||
@@ -25,24 +19,23 @@ result[k] = v; | ||
} | ||
} | ||
function create(factory) { | ||
}; | ||
exports.create = (factory) => { | ||
const result = {}; | ||
forEach(factory, (k, propertyFactory) => { | ||
exports.forEach(factory, (propertyFactory, k) => { | ||
setProperty(result, k, propertyFactory(k)); | ||
}); | ||
return fromMutableOptional(result); | ||
} | ||
exports.create = create; | ||
function copyCreate(source, factory) { | ||
}; | ||
exports.copyCreate = (source, factory) => { | ||
const result = {}; | ||
forEach(source, (k, v) => { | ||
exports.forEach(source, (v, k) => { | ||
setProperty(result, k, v); | ||
}); | ||
let changes = false; | ||
forEach(factory, (k, propertyFactory) => { | ||
exports.forEach(factory, (propertyFactory, k) => { | ||
// tslint:disable-next-line:strict-type-predicates | ||
if (propertyFactory !== undefined) { | ||
const sourceK = source[k]; | ||
const newProperty = propertyFactory(k, sourceK); | ||
if (sourceK !== newProperty) { | ||
setProperty(result, k, newProperty); | ||
const sourceValue = source[k]; | ||
const newValue = propertyFactory(sourceValue, k); | ||
if (sourceValue !== newValue) { | ||
setProperty(result, k, newValue); | ||
changes = true; | ||
@@ -53,3 +46,2 @@ } | ||
return changes ? fromMutableOptional(result) : source; | ||
} | ||
exports.copyCreate = copyCreate; | ||
}; |
{ | ||
"name": "@ts-common/property-set", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A set of properties", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
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
14710
60