Comparing version 0.0.5 to 0.0.6
import { OptionalObject, WritablePart } from './types'; | ||
export declare function copyWith<T>(model: T, values: OptionalObject<WritablePart<T>>): T; | ||
export declare function copyWith<T extends Record<string, any>>(model: T, values: OptionalObject<WritablePart<T>>): Readonly<T>; |
@@ -1,2 +0,2 @@ | ||
import { JsonResponse, Type } from '.'; | ||
export declare function fromJson<T>(KlassName: Type<T>, json: JsonResponse): T; | ||
import { JsonResponse, Type } from './types'; | ||
export declare function fromJson<T extends Record<string, any>>(KlassName: Type<T>, json: JsonResponse): Readonly<T>; |
export * from './types'; | ||
export * from './transform-api'; | ||
export { copyWith } from './copy-with'; | ||
export { unfreeze } from './utils/unfreeze'; | ||
export { fromJson } from './from-json'; | ||
export { mutable } from './mutable'; | ||
export { nullable } from './nullable'; | ||
export { make } from './make'; | ||
export { defineTransform } from './transform-map'; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["restful-resource"]={})}(this,(function(e){"use strict";function t(e){return e.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map((e=>e.toLowerCase())).join("_")}function n(e,n){function r(e,t,n){return null==e?n:t()}return{nullOr:function(e,t){return r(e,t,null)},withDefault:r,fromSnakeCaseKeys:function(r){const o={};if(r)r.forEach((e=>{o[e]=n[t(e)]}));else{Object.getOwnPropertyNames(e).forEach((e=>{o[e]=n[t(e)]}))}return o}}}function r(e,t){const n=Object.create(Object.getPrototypeOf(e),Object.getOwnPropertyDescriptors(e)),r=Object.assign(Object.assign({},n),t);return Object.keys(r).forEach((e=>{Object.defineProperty(n,e,{value:r[e]})})),n}const o=new Map;e.copyWith=r,e.createTransformAPI=n,e.defineTransform=function(e,t){o.set(e,t)},e.fromJson=function(e,t){const r=new e,c=Object.getOwnPropertyNames(r),f=(u=e,o.get(u));var u;const i=f?f(t,n(r,t)):{};return c.forEach((e=>{let n;n=e in i?i[e]:null==t?null:t[e],Object.defineProperty(r,e,{value:n,writable:!0})})),r},e.make=function(e,t){return r(new e,t)},e.mutable=function(e,t){const n=new e;return Object.getOwnPropertyNames(n).forEach((e=>{Object.defineProperty(n,e,{value:t&&e in t?t[e]:null})})),n},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["restful-resource"]={})}(this,(function(e){"use strict";function t(e){return e.replace(/\W+/g," ").split(/ |\B(?=[A-Z])/).map((e=>e.toLowerCase())).join("_")}function n(e,n){function r(e,t,n){return null==e?n:t()}return{nullOr:function(e,t){return r(e,t,null)},withDefault:r,fromSnakeCaseKeys:function(r){const o={};if(r)r.forEach((e=>{o[e]=n[t(e)]}));else{Object.getOwnPropertyNames(e).forEach((e=>{o[e]=n[t(e)]}))}return o}}}function r(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}function o(e){return Object.keys(e).forEach((t=>{if(r(e[t]))return o(e[t])})),Object.freeze(e)}function c(e){let t={};for(const n in e)e.hasOwnProperty(n)&&(t[n]=r(e[n])?c(e[n]):e[n]);return Object.setPrototypeOf(t,Object.getPrototypeOf(e)),t}function f(e,t){const n=Object.isFrozen(e)?c(e):e,r=Object.create(Object.getPrototypeOf(n),Object.getOwnPropertyDescriptors(n)),f=Object.assign(Object.assign({},r),t);return Object.keys(f).forEach((e=>{Object.defineProperty(r,e,{value:f[e]})})),o(r)}const u=new Map;e.copyWith=f,e.createTransformAPI=n,e.defineTransform=function(e,t){u.set(e,t)},e.fromJson=function(e,t){const r=new e,c=Object.getOwnPropertyNames(r),f=(i=e,u.get(i));var i;const s=f?f(t,n(r,t)):{};return c.forEach((e=>{let n;n=e in s?s[e]:null==t?null:t[e],Object.defineProperty(r,e,{value:n})})),o(r)},e.make=function(e,t){return f(new e,t)},e.nullable=function(e,t){const n=new e;return Object.getOwnPropertyNames(n).forEach((e=>{Object.defineProperty(n,e,{value:t&&e in t?t[e]:null})})),n},e.unfreeze=c,Object.defineProperty(e,"__esModule",{value:!0})})); |
import { Type, WritablePart } from './types'; | ||
export declare function make<T>(Model: Type<T>, values: WritablePart<T>): T; | ||
export declare function make<T>(Model: Type<T>, values: WritablePart<T>): Readonly<T>; |
{ | ||
"name": "dtto", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "", | ||
"type": "module", | ||
"source": "src/index.ts", | ||
"main": "dist/index.js", | ||
"umd:main": "dist/index.umd.js", | ||
"typings": "dist/index.d.ts", | ||
"exports": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.js", | ||
"default": "./dist/index.modern.js" | ||
}, | ||
"module": "./dist/index.js", | ||
"unpkg": "./dist/index.umd.js", | ||
"types": "dist/index.d.ts", | ||
"module": "./dist/index.cjs", | ||
"scripts": { | ||
@@ -18,0 +9,0 @@ "test": "jest", |
import { House } from '../test/house'; | ||
import { Person } from '../test/person'; | ||
import { copyWith } from './copy-with'; | ||
import { fromJson } from './from-json'; | ||
@@ -30,2 +31,9 @@ describe('copyWith', () => { | ||
}); | ||
it('should work on a frozen object', () => { | ||
const tom = fromJson(Person, { firstName: 'Tom', lastName: 'Jones', age: 65 }); | ||
const tom2 = copyWith(tom, { firstName: 'Thomas' }); | ||
expect(tom2).toMatchObject({ firstName: 'Thomas', lastName: 'Jones', age: 65, fullName: 'Thomas Jones' }); | ||
}); | ||
}); |
@@ -1,3 +0,4 @@ | ||
import { TransformResponse } from './transform-api'; | ||
import { OptionalObject, WritablePart } from './types'; | ||
import { Mutable, OptionalObject, WritablePart } from './types'; | ||
import { deepFreeze } from './utils/deep-freeze'; | ||
import { unfreeze } from './utils/unfreeze'; | ||
@@ -9,4 +10,8 @@ /** | ||
*/ | ||
export function copyWith<T>(model: T, values: OptionalObject<WritablePart<T>>): T { | ||
const clone = Object.create(Object.getPrototypeOf(model), Object.getOwnPropertyDescriptors(model)); | ||
export function copyWith<T extends Record<string, any>>( | ||
model: T, | ||
values: OptionalObject<WritablePart<T>>, | ||
): Readonly<T> { | ||
const m = Object.isFrozen(model) ? unfreeze(model) : model; | ||
const clone = Object.create(Object.getPrototypeOf(m), Object.getOwnPropertyDescriptors(m)); | ||
const fields = { | ||
@@ -23,3 +28,3 @@ ...clone, | ||
return clone; | ||
return deepFreeze(clone); | ||
} |
@@ -1,3 +0,5 @@ | ||
import { createTransformAPI, JsonResponse, Type } from '.'; | ||
import { createTransformAPI } from './transform-api'; | ||
import { getTransform } from './transform-map'; | ||
import { JsonResponse, Type } from './types'; | ||
import { deepFreeze } from './utils/deep-freeze'; | ||
import { isNullOrUndefined } from './utils/is-null-or-undefined'; | ||
@@ -8,3 +10,3 @@ | ||
*/ | ||
export function fromJson<T>(KlassName: Type<T>, json: JsonResponse): T { | ||
export function fromJson<T extends Record<string, any>>(KlassName: Type<T>, json: JsonResponse): Readonly<T> { | ||
const instance = new KlassName() as T; | ||
@@ -26,7 +28,6 @@ const propertyKeys = Object.getOwnPropertyNames(instance); | ||
value, | ||
writable: true, | ||
}); | ||
}); | ||
return instance as T; | ||
return deepFreeze(instance); | ||
} |
export * from './types'; | ||
export * from './transform-api'; | ||
export { copyWith } from './copy-with'; | ||
export { unfreeze } from './utils/unfreeze'; | ||
export { fromJson } from './from-json'; | ||
export { mutable } from './mutable'; | ||
export { nullable } from './nullable'; | ||
export { make } from './make'; | ||
export { defineTransform } from './transform-map'; |
@@ -8,3 +8,3 @@ import { copyWith } from './copy-with'; | ||
*/ | ||
export function make<T>(Model: Type<T>, values: WritablePart<T>): T { | ||
export function make<T>(Model: Type<T>, values: WritablePart<T>): Readonly<T> { | ||
const instance = new Model() as T; | ||
@@ -11,0 +11,0 @@ |
49530
62
898
No