@wixc3/common
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -16,3 +16,3 @@ export declare function exclude<R>(...excluded: R[]): <T>(t: T) => t is Exclude<T, R>; | ||
}; | ||
export declare function isPlainObject(value: unknown): value is object; | ||
export declare function isPlainObject(value: unknown): value is Record<string | number | symbol, any>; | ||
export declare const reportError: (ex: unknown) => void; | ||
@@ -19,0 +19,0 @@ /** |
@@ -39,2 +39,17 @@ "use strict"; | ||
}); | ||
describe('isPlainObject', () => { | ||
it('returns true for POJO', () => { | ||
class A { | ||
} | ||
(0, chai_1.expect)((0, objects_1.isPlainObject)({})).to.be.true; | ||
(0, chai_1.expect)((0, objects_1.isPlainObject)(null)).to.be.false; | ||
(0, chai_1.expect)((0, objects_1.isPlainObject)(undefined)).to.be.false; | ||
(0, chai_1.expect)((0, objects_1.isPlainObject)(new A())).to.be.false; | ||
(0, chai_1.expect)((0, objects_1.isPlainObject)(new Map())).to.be.false; | ||
}); | ||
it('allows direct access to fields', () => { | ||
const a = { a: 1 }; | ||
(0, chai_1.expect)((0, objects_1.isPlainObject)(a) && a.a).to.eql(1); | ||
}); | ||
}); | ||
//# sourceMappingURL=objects.unit.js.map |
{ | ||
"name": "@wixc3/common", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Common utils, usable in all environments", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -43,3 +43,3 @@ import { sleep } from 'promise-assist'; | ||
export function isPlainObject(value: unknown): value is object { | ||
export function isPlainObject(value: unknown): value is Record<string|number|symbol, any>{ | ||
return value !== null | ||
@@ -46,0 +46,0 @@ && typeof value === 'object' |
import { expect } from 'chai'; | ||
import { defaults, pick, remap } from '../objects'; | ||
import { defaults, isPlainObject, pick, remap } from '../objects'; | ||
@@ -12,29 +12,44 @@ describe('pick', () => { | ||
describe(`defaults`, ()=>{ | ||
it(`merges to POJOs`,()=>{ | ||
expect(defaults({a:0, b:1},{a:1, b:2,c:3})).to.eql({a:0, b:1, c:3}) | ||
describe(`defaults`, () => { | ||
it(`merges to POJOs`, () => { | ||
expect(defaults({ a: 0, b: 1 }, { a: 1, b: 2, c: 3 })).to.eql({ a: 0, b: 1, c: 3 }) | ||
}) | ||
it(`deep=true (default)`,()=>{ | ||
expect(defaults({a:{a:10,b:20}},{a:{a:20,c:30}})).to.eql({a:{a:10,b:20,c:30}}) | ||
}) | ||
it(`deep=false`,()=>{ | ||
expect(defaults({a:{a:10}},{a:{b:20}}, false)).to.eql({a:{a:10}}) | ||
}) | ||
it(`shouldUseDefault arg`,()=>{ | ||
expect(defaults({a:{a:10,b:20}},{a:{a:20,c:30}}, true, i=> i===10)).to.eql({a:{a:20,b:20,c:30}}) | ||
expect(defaults({a:{a:10,b:20}},{a:{a:20,c:30}}, true, (_,key)=> key==='a.a')).to.eql({a:{a:20,b:20,c:30}}) | ||
it(`deep=true (default)`, () => { | ||
expect(defaults({ a: { a: 10, b: 20 } }, { a: { a: 20, c: 30 } })).to.eql({ a: { a: 10, b: 20, c: 30 } }) | ||
}) | ||
it(`deep=false`, () => { | ||
expect(defaults({ a: { a: 10 } }, { a: { b: 20 } }, false)).to.eql({ a: { a: 10 } }) | ||
}) | ||
it(`shouldUseDefault arg`, () => { | ||
expect(defaults({ a: { a: 10, b: 20 } }, { a: { a: 20, c: 30 } }, true, i => i === 10)).to.eql({ a: { a: 20, b: 20, c: 30 } }) | ||
expect(defaults({ a: { a: 10, b: 20 } }, { a: { a: 20, c: 30 } }, true, (_, key) => key === 'a.a')).to.eql({ a: { a: 20, b: 20, c: 30 } }) | ||
}) | ||
}) | ||
describe('remap', ()=>{ | ||
it('changes given keys', ()=>{ | ||
const input = {a:0, b:1}; | ||
const output:{c:number, b:number} = remap(input, {a:'c'}) | ||
expect(output).to.eql({c:0, b:1}) | ||
describe('remap', () => { | ||
it('changes given keys', () => { | ||
const input = { a: 0, b: 1 }; | ||
const output: { c: number, b: number } = remap(input, { a: 'c' }) | ||
expect(output).to.eql({ c: 0, b: 1 }) | ||
}) | ||
it('removes keys mapped to remap.DELETE', ()=>{ | ||
const input = {a:0, b:1}; | ||
const output:{b:number} = remap(input, {a: remap.DELETE}) | ||
expect(output).to.eql({b:1}) | ||
it('removes keys mapped to remap.DELETE', () => { | ||
const input = { a: 0, b: 1 }; | ||
const output: { b: number } = remap(input, { a: remap.DELETE }) | ||
expect(output).to.eql({ b: 1 }) | ||
}) | ||
}) | ||
describe('isPlainObject', () => { | ||
it('returns true for POJO', () => { | ||
class A { } | ||
expect(isPlainObject({})).to.be.true | ||
expect(isPlainObject(null)).to.be.false | ||
expect(isPlainObject(undefined)).to.be.false | ||
expect(isPlainObject(new A())).to.be.false | ||
expect(isPlainObject(new Map())).to.be.false | ||
}) | ||
it('allows direct access to fields', () => { | ||
const a = { a: 1 } | ||
expect(isPlainObject(a) && a.a).to.eql(1) | ||
}) | ||
}) |
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
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
264902
4772