@curong/object
Advanced tools
Comparing version 0.0.0-alpha.1 to 0.0.0-alpha.2
@@ -0,2 +1,75 @@ | ||
/** 属性名的类型,属性名是 `string`、`number` 和 `symbol` 类型中的某一个类型。 */ | ||
declare type PropertyKey = string | number | symbol; | ||
/** | ||
* 对象的默认类型 | ||
* | ||
* 对象的属性名是 `string` 或 `number` 或 `symbol` 类型,对象的值是 `any` 类型。 | ||
* | ||
* @note `Record<PropertyKey, any>` 相当于 `{ [key in PropertyKey]: any }` | ||
*/ | ||
declare type ObjectType = Record<PropertyKey, any>; | ||
/** | ||
* 创建一个纯对象,该对象的原型是 `null` | ||
* | ||
* @param properties 包含一个或多个属性描述符的 JavaScript 对象 | ||
* @returns 返回一个纯对象 | ||
* @example | ||
* | ||
* ```javascript | ||
* const obj = createWithNull({ | ||
* a: { | ||
* get() { | ||
* return 1; | ||
* } | ||
* } | ||
* }); | ||
* | ||
* console.log(obj.__proto__); // undefined | ||
* console.log(obj.a); // 1 | ||
* ``` | ||
*/ | ||
declare function createWithNull(properties?: PropertyDescriptorMap & ThisType<any>): ObjectType; | ||
/** | ||
* 从一个对象中删除一些属性 | ||
* | ||
* @param obj 对象 | ||
* @param deleteKeys 要从对象中的删除的属性字符串数组 | ||
* @returns 返回处理好的对象 | ||
* @example | ||
* | ||
* ```javascript | ||
* let obj = { a: 1, b: 2, c: 3 }; | ||
* // 请在删除属性时接受该函数的返回值 | ||
* obj = deleteAttrs(obj, ['b']); | ||
* console.log(obj); // { a: 1, c: 3 } | ||
* ``` | ||
*/ | ||
declare function deleteAttrs<T extends ObjectType>(obj: T, deleteKeys: Array<keyof T>): T; | ||
/** | ||
* 基于一个对象派生出和它长得一模一样的对象。在派生出的对象中,它的属性和 `obj` 是完全一样的。 | ||
* | ||
* 需要注意的是,对象的派生操作不是深拷贝,而是进行了浅拷贝。 | ||
* 并且,在派生的时候,也是仅仅处理 `obj` 顶层的属性,并没有涉及到递归操作。 | ||
* 如果想对对象进行深拷贝的话,请使用 `@curong/util` 下面的 `copy` 方法。 | ||
* | ||
* @param obj 基于哪个对象派生 | ||
* @param deleteKeys 要从对象中删除哪个属性,删除的属性将不在派生的对象中存在 | ||
* @returns 返回派生好的对象 | ||
* @example | ||
* | ||
* ```javascript | ||
* const obj = { a: 1, b: 2, c: 3, d: { e: true } }; | ||
* const ret = derive(obj); | ||
* | ||
* // { a: 1, b: 2, c: 3, d: { e: true } } | ||
* console.log(ret); | ||
* console.log(ret.d === obj.d); // true | ||
* ``` | ||
*/ | ||
declare function derive<T extends ObjectType>(obj: T, deleteKeys?: Array<keyof T>): any; | ||
/** | ||
* 是不是当前对象的原型上的属性 | ||
@@ -29,47 +102,55 @@ * | ||
declare type PropertyKey = string | number; | ||
/** 对象的默认类型 */ | ||
declare type ObjectType = { | ||
[key in PropertyKey]: any; | ||
}; | ||
/** | ||
* 从一个对象中删除一些属性 | ||
* 通过浅比较的方式比较两个对象中的属性。(仅比较对象中的一层属性) | ||
* | ||
* @param obj 对象 | ||
* @param keys 要从对象中的删除的属性字符串数组 | ||
* @returns 返回处理好的对象 | ||
* @example | ||
* @param objA 一个对象 | ||
* @param objB 另一个对象 | ||
* @returns 如果相等则返回 `true`,否则返回 `false` | ||
* @example ```` | ||
* | ||
* ### 两个空对象的结果为 `true` | ||
* | ||
* ```javascript | ||
* let obj = { a: 1, b: 2, c: 3 }; | ||
* // 请在删除属性时接受该函数的返回值 | ||
* obj = deleteAttrs(obj, ['b']); | ||
* console.log(obj); // { a: 1, c: 3 } | ||
* console.log(shallowEqual({}, {})); // true | ||
* ``` | ||
*/ | ||
declare function deleteAttrs<T extends ObjectType>(obj: T, keys: Array<keyof T>): T; | ||
/** | ||
* 创建一个纯对象,该对象的原型是 `null` | ||
* | ||
* @param properties 包含一个或多个属性描述符的 JavaScript 对象 | ||
* @returns 返回一个纯对象 | ||
* @example | ||
* ### 对象中的属性相同结果为 `true` | ||
* | ||
* ```javascript | ||
* const obj = createWithNull({ | ||
* a: { | ||
* get() { | ||
* return 1; | ||
* } | ||
* } | ||
* }); | ||
* const o1 = { name: 'wtklbm' }; | ||
* const o2 = { name: 'wtklbm' }; | ||
* | ||
* console.log(obj.__proto__); // undefined | ||
* console.log(obj.a); // 1 | ||
* console.log(shallowEqual(o1, o2)); // true | ||
* ``` | ||
* | ||
* ### 对象中的属性个数不同结果为 `false` | ||
* | ||
* ```javascript | ||
* const o1 = { name: 'wtklbm' }; | ||
* const o2 = { | ||
* name: 'wtklbm', | ||
* data: 'text' | ||
* }; | ||
* | ||
* console.log(shallowEqual(o1, o2)); // false | ||
* ``` | ||
* | ||
* ### 对象中的属性引用地址不同结果为 `false` | ||
* | ||
* ```javascript | ||
* const o1 = { | ||
* name: { data: 'wtklbm' } | ||
* }; | ||
* | ||
* const o2 = { | ||
* name: { data: 'wtklbm' } | ||
* }; | ||
* | ||
* console.log(shallowEqual(o1, o2)); // false | ||
* ``` | ||
* | ||
* @see https://github.com/facebook/react/blob/2c9fef32db5c9a342a1a60c34217ffc9ae087fbb/packages/shared/shallowEqual.js | ||
*/ | ||
declare function createWithNull(properties?: PropertyDescriptorMap & ThisType<any>): ObjectType; | ||
declare function shallowEqual(objA: ObjectType | null, objB: ObjectType | null): boolean; | ||
export { createWithNull, deleteAttrs, isPrototypeProperty }; | ||
export { createWithNull, deleteAttrs, derive, isPrototypeProperty, shallowEqual }; |
@@ -56,18 +56,14 @@ 'use strict'; | ||
function isPrototypeProperty(object, key) { | ||
return (isTypeofObject(object) && !object.hasOwnProperty(key) && key in object); | ||
function createWithNull(properties) { | ||
if (isObject(properties)) { | ||
return Object.create(null, properties); | ||
} | ||
return Object.create(null); | ||
} | ||
function deleteAttrs(obj, keys) { | ||
function derive(obj, deleteKeys) { | ||
const ret = Object.create(Object.getPrototypeOf(obj)); | ||
const keysSet = new Set(deleteKeys); | ||
const objKeys = Reflect.ownKeys(obj); | ||
let key; | ||
try { | ||
while (!isUndefined((key = keys.shift()))) { | ||
delete obj[key]; | ||
} | ||
return obj; | ||
} | ||
catch (e) { } | ||
const ret = Object.create(Object.getPrototypeOf(obj)); | ||
const keysSet = new Set(keys); | ||
const objKeys = Object.getOwnPropertyNames(obj); | ||
while (!isUndefined((key = objKeys.shift()))) { | ||
@@ -81,11 +77,50 @@ if (!keysSet.has(key)) { | ||
function createWithNull(properties) { | ||
if (isObject(properties)) { | ||
return Object.create(null, properties); | ||
function deleteAttrs(obj, deleteKeys) { | ||
let key; | ||
while (!isUndefined((key = deleteKeys.pop()))) { | ||
if (!Reflect.deleteProperty(obj, key)) { | ||
deleteKeys.push(key); | ||
return derive(obj, deleteKeys); | ||
} | ||
} | ||
return Object.create(null); | ||
return obj; | ||
} | ||
function isPrototypeProperty(object, key) { | ||
return (isTypeofObject(object) && !object.hasOwnProperty(key) && key in object); | ||
} | ||
const _is = (x, y) => { | ||
return (x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y); | ||
}; | ||
const is = typeof Object.is === 'function' ? Object.is : _is; | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) { | ||
return true; | ||
} | ||
if (typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null) { | ||
return false; | ||
} | ||
const keysA = Object.keys(objA); | ||
const keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < keysA.length; i++) { | ||
if (!hasOwnProperty.call(objB, keysA[i]) || | ||
!is(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
exports.createWithNull = createWithNull; | ||
exports.deleteAttrs = deleteAttrs; | ||
exports.derive = derive; | ||
exports.isPrototypeProperty = isPrototypeProperty; | ||
exports.shallowEqual = shallowEqual; |
@@ -52,18 +52,14 @@ import 'stream'; | ||
function isPrototypeProperty(object, key) { | ||
return (isTypeofObject(object) && !object.hasOwnProperty(key) && key in object); | ||
function createWithNull(properties) { | ||
if (isObject(properties)) { | ||
return Object.create(null, properties); | ||
} | ||
return Object.create(null); | ||
} | ||
function deleteAttrs(obj, keys) { | ||
function derive(obj, deleteKeys) { | ||
const ret = Object.create(Object.getPrototypeOf(obj)); | ||
const keysSet = new Set(deleteKeys); | ||
const objKeys = Reflect.ownKeys(obj); | ||
let key; | ||
try { | ||
while (!isUndefined((key = keys.shift()))) { | ||
delete obj[key]; | ||
} | ||
return obj; | ||
} | ||
catch (e) { } | ||
const ret = Object.create(Object.getPrototypeOf(obj)); | ||
const keysSet = new Set(keys); | ||
const objKeys = Object.getOwnPropertyNames(obj); | ||
while (!isUndefined((key = objKeys.shift()))) { | ||
@@ -77,9 +73,46 @@ if (!keysSet.has(key)) { | ||
function createWithNull(properties) { | ||
if (isObject(properties)) { | ||
return Object.create(null, properties); | ||
function deleteAttrs(obj, deleteKeys) { | ||
let key; | ||
while (!isUndefined((key = deleteKeys.pop()))) { | ||
if (!Reflect.deleteProperty(obj, key)) { | ||
deleteKeys.push(key); | ||
return derive(obj, deleteKeys); | ||
} | ||
} | ||
return Object.create(null); | ||
return obj; | ||
} | ||
export { createWithNull, deleteAttrs, isPrototypeProperty }; | ||
function isPrototypeProperty(object, key) { | ||
return (isTypeofObject(object) && !object.hasOwnProperty(key) && key in object); | ||
} | ||
const _is = (x, y) => { | ||
return (x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y); | ||
}; | ||
const is = typeof Object.is === 'function' ? Object.is : _is; | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) { | ||
return true; | ||
} | ||
if (typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null) { | ||
return false; | ||
} | ||
const keysA = Object.keys(objA); | ||
const keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < keysA.length; i++) { | ||
if (!hasOwnProperty.call(objB, keysA[i]) || | ||
!is(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
export { createWithNull, deleteAttrs, derive, isPrototypeProperty, shallowEqual }; |
@@ -1,1 +0,1 @@ | ||
"use strict";function t(t){return function(t){return"string"==typeof t}(t)||function(t){return"object"==typeof t&&"String"===e(t)}(t)}function e(e,r=!0){const n=Object.prototype.toString;let o=null;return function(t){return"function"==typeof t}(n)&&t(o=n.call(e))&&function(t){return!0===t}(r)&&o.startsWith("[object ")&&o.endsWith("]")?o.slice(8,-1):o}function r(t){return void 0===t}Object.defineProperty(exports,"__esModule",{value:!0}),require("stream"),exports.createWithNull=function(t){return"Object"===e(t)?Object.create(null,t):Object.create(null)},exports.deleteAttrs=function(t,e){let n;try{for(;!r(n=e.shift());)delete t[n];return t}catch(t){}const o=Object.create(Object.getPrototypeOf(t)),c=new Set(e),u=Object.getOwnPropertyNames(t);for(;!r(n=u.shift());)c.has(n)||Object.defineProperty(o,n,Object.getOwnPropertyDescriptor(t,n));return o},exports.isPrototypeProperty=function(t,e){return"object"==typeof(r=t)&&!function(t){return null===t}(r)&&!t.hasOwnProperty(e)&&e in t;var r}; | ||
"use strict";function t(t){return function(t){return"string"==typeof t}(t)||function(t){return"object"==typeof t&&"String"===e(t)}(t)}function e(e,r=!0){const n=Object.prototype.toString;let o=null;return function(t){return"function"==typeof t}(n)&&t(o=n.call(e))&&function(t){return!0===t}(r)&&o.startsWith("[object ")&&o.endsWith("]")?o.slice(8,-1):o}function r(t){return void 0===t}function n(t,e){const n=Object.create(Object.getPrototypeOf(t)),o=new Set(e),c=Reflect.ownKeys(t);let u;for(;!r(u=c.shift());)o.has(u)||Object.defineProperty(n,u,Object.getOwnPropertyDescriptor(t,u));return n}Object.defineProperty(exports,"__esModule",{value:!0}),require("stream");const o="function"==typeof Object.is?Object.is:(t,e)=>t===e&&(0!==t||1/t==1/e)||t!=t&&e!=e,c=Object.prototype.hasOwnProperty;exports.createWithNull=function(t){return"Object"===e(t)?Object.create(null,t):Object.create(null)},exports.deleteAttrs=function(t,e){let o;for(;!r(o=e.pop());)if(!Reflect.deleteProperty(t,o))return e.push(o),n(t,e);return t},exports.derive=n,exports.isPrototypeProperty=function(t,e){return"object"==typeof(r=t)&&!function(t){return null===t}(r)&&!t.hasOwnProperty(e)&&e in t;var r},exports.shallowEqual=function(t,e){if(o(t,e))return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;const r=Object.keys(t),n=Object.keys(e);if(r.length!==n.length)return!1;for(let n=0;n<r.length;n++)if(!c.call(e,r[n])||!o(t[r[n]],e[r[n]]))return!1;return!0}; |
@@ -56,18 +56,14 @@ (function (global, factory) { | ||
function isPrototypeProperty(object, key) { | ||
return (isTypeofObject(object) && !object.hasOwnProperty(key) && key in object); | ||
function createWithNull(properties) { | ||
if (isObject(properties)) { | ||
return Object.create(null, properties); | ||
} | ||
return Object.create(null); | ||
} | ||
function deleteAttrs(obj, keys) { | ||
function derive(obj, deleteKeys) { | ||
const ret = Object.create(Object.getPrototypeOf(obj)); | ||
const keysSet = new Set(deleteKeys); | ||
const objKeys = Reflect.ownKeys(obj); | ||
let key; | ||
try { | ||
while (!isUndefined((key = keys.shift()))) { | ||
delete obj[key]; | ||
} | ||
return obj; | ||
} | ||
catch (e) { } | ||
const ret = Object.create(Object.getPrototypeOf(obj)); | ||
const keysSet = new Set(keys); | ||
const objKeys = Object.getOwnPropertyNames(obj); | ||
while (!isUndefined((key = objKeys.shift()))) { | ||
@@ -81,12 +77,51 @@ if (!keysSet.has(key)) { | ||
function createWithNull(properties) { | ||
if (isObject(properties)) { | ||
return Object.create(null, properties); | ||
function deleteAttrs(obj, deleteKeys) { | ||
let key; | ||
while (!isUndefined((key = deleteKeys.pop()))) { | ||
if (!Reflect.deleteProperty(obj, key)) { | ||
deleteKeys.push(key); | ||
return derive(obj, deleteKeys); | ||
} | ||
} | ||
return Object.create(null); | ||
return obj; | ||
} | ||
function isPrototypeProperty(object, key) { | ||
return (isTypeofObject(object) && !object.hasOwnProperty(key) && key in object); | ||
} | ||
const _is = (x, y) => { | ||
return (x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y); | ||
}; | ||
const is = typeof Object.is === 'function' ? Object.is : _is; | ||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) { | ||
return true; | ||
} | ||
if (typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null) { | ||
return false; | ||
} | ||
const keysA = Object.keys(objA); | ||
const keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < keysA.length; i++) { | ||
if (!hasOwnProperty.call(objB, keysA[i]) || | ||
!is(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
exports.createWithNull = createWithNull; | ||
exports.deleteAttrs = deleteAttrs; | ||
exports.derive = derive; | ||
exports.isPrototypeProperty = isPrototypeProperty; | ||
exports.shallowEqual = shallowEqual; | ||
@@ -93,0 +128,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("stream")):"function"==typeof define&&define.amd?define(["exports","stream"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).object={})}(this,(function(t){"use strict";function e(t){return function(t){return"string"==typeof t}(t)||function(t){return"object"==typeof t&&"String"===n(t)}(t)}function n(t,n=!0){const r=Object.prototype.toString;let o=null;return function(t){return"function"==typeof t}(r)&&e(o=r.call(t))&&function(t){return!0===t}(n)&&o.startsWith("[object ")&&o.endsWith("]")?o.slice(8,-1):o}function r(t){return void 0===t}t.createWithNull=function(t){return"Object"===n(t)?Object.create(null,t):Object.create(null)},t.deleteAttrs=function(t,e){let n;try{for(;!r(n=e.shift());)delete t[n];return t}catch(t){}const o=Object.create(Object.getPrototypeOf(t)),c=new Set(e),i=Object.getOwnPropertyNames(t);for(;!r(n=i.shift());)c.has(n)||Object.defineProperty(o,n,Object.getOwnPropertyDescriptor(t,n));return o},t.isPrototypeProperty=function(t,e){return"object"==typeof(n=t)&&!function(t){return null===t}(n)&&!t.hasOwnProperty(e)&&e in t;var n},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("stream")):"function"==typeof define&&define.amd?define(["exports","stream"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).object={})}(this,(function(t){"use strict";function e(t){return function(t){return"string"==typeof t}(t)||function(t){return"object"==typeof t&&"String"===n(t)}(t)}function n(t,n=!0){const r=Object.prototype.toString;let o=null;return function(t){return"function"==typeof t}(r)&&e(o=r.call(t))&&function(t){return!0===t}(n)&&o.startsWith("[object ")&&o.endsWith("]")?o.slice(8,-1):o}function r(t){return void 0===t}function o(t,e){const n=Object.create(Object.getPrototypeOf(t)),o=new Set(e),c=Reflect.ownKeys(t);let u;for(;!r(u=c.shift());)o.has(u)||Object.defineProperty(n,u,Object.getOwnPropertyDescriptor(t,u));return n}const c="function"==typeof Object.is?Object.is:(t,e)=>t===e&&(0!==t||1/t==1/e)||t!=t&&e!=e,u=Object.prototype.hasOwnProperty;t.createWithNull=function(t){return"Object"===n(t)?Object.create(null,t):Object.create(null)},t.deleteAttrs=function(t,e){let n;for(;!r(n=e.pop());)if(!Reflect.deleteProperty(t,n))return e.push(n),o(t,e);return t},t.derive=o,t.isPrototypeProperty=function(t,e){return"object"==typeof(n=t)&&!function(t){return null===t}(n)&&!t.hasOwnProperty(e)&&e in t;var n},t.shallowEqual=function(t,e){if(c(t,e))return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;const n=Object.keys(t),r=Object.keys(e);if(n.length!==r.length)return!1;for(let r=0;r<n.length;r++)if(!u.call(e,n[r])||!c(t[n[r]],e[n[r]]))return!1;return!0},Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "@curong/object", | ||
"version": "0.0.0-alpha.1", | ||
"version": "0.0.0-alpha.2", | ||
"license": "MIT", | ||
@@ -31,6 +31,6 @@ "author": { | ||
"dependencies": { | ||
"@curong/types": "^0.0.0-alpha.1" | ||
"@curong/types": "^0.0.0-alpha.2" | ||
}, | ||
"sideEffects": false, | ||
"gitHead": "17336f4593ad9cbe4e46e400fa22ea0074e82ea1" | ||
"gitHead": "c6ba60b39efca33e0df2139e1376cb3ff8babba0" | ||
} |
@@ -9,1 +9,2 @@ # `@curong/object` | ||
- `isPrototypeProperty`: 是不是当前对象的原型上的属性 | ||
- `shallowEqual`: 通过浅比较的方式比较两个对象中的属性。(仅比较对象中的一层属性) |
20471
478
10
Updated@curong/types@^0.0.0-alpha.2