本文整理汇总了TypeScript中@dojo/shim/object.keys函数的典型用法代码示例。如果您正苦于以下问题:TypeScript keys函数的具体用法?TypeScript keys怎么用?TypeScript keys使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了keys函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getComparableObjects
export function getComparableObjects(a: any, b: any, options: DiffOptions) {
const { ignoreProperties = [], ignorePropertyValues = [] } = options;
const ignore = new Set<string>();
const keep = new Set<string>();
const isIgnoredProperty = Array.isArray(ignoreProperties) ? (name: string) => {
return ignoreProperties.some((value) => typeof value === 'string' ? name === value : value.test(name));
} : (name: string) => ignoreProperties(name, a, b);
const comparableA = keys(a).reduce((obj, name) => {
if (isIgnoredProperty(name) ||
hasOwnProperty.call(b, name) && isIgnoredPropertyValue(name, a, b, ignorePropertyValues)) {
ignore.add(name);
return obj;
}
keep.add(name);
obj[name] = a[name];
return obj;
}, {} as { [key: string]: any });
const comparableB = keys(b).reduce((obj, name) => {
if (ignore.has(name) || !keep.has(name) && isIgnoredProperty(name)) {
return obj;
}
obj[name] = b[name];
return obj;
}, {} as { [key: string]: any });
return { comparableA, comparableB, ignore };
}
示例2: diffPlainObject
/**
* Internal function that detects the differences between plain objects and returns a set of patch records that
* describe the differences
*
* @param a The first plain object to compare to
* @param b The second plain bject to compare to
* @param options An options bag that allows configuration of the behaviour of `diffPlainObject()`
*/
function diffPlainObject(a: any, b: any, options: DiffOptions): (ConstructRecord | PatchRecord)[] {
const { allowFunctionValues = false, ignorePropertyValues = [] } = options;
const patchRecords: (ConstructRecord | PatchRecord)[] = [];
const { comparableA, comparableB } = getComparableObjects(a, b, options);
/* look for keys in a that are different from b */
keys(comparableA).reduce((patchRecords, name) => {
const valueA = a[name];
const valueB = b[name];
const bHasOwnProperty = hasOwnProperty.call(comparableB, name);
if (
bHasOwnProperty &&
(valueA === valueB || (allowFunctionValues && typeof valueA === 'function' && typeof valueB === 'function'))
) {
/* not different */
/* when `allowFunctionValues` is true, functions are simply considered to be equal by `typeof` */
return patchRecords;
}
const type = bHasOwnProperty ? 'update' : 'add';
const isValueAArray = isArray(valueA);
const isValueAPlainObject = isPlainObject(valueA);
if (isValueAArray || isValueAPlainObject) {
/* non-primitive values we can diff */
/* this is a bit complicated, but essentially if valueA and valueB are both arrays or plain objects, then
* we can diff those two values, if not, then we need to use an empty array or an empty object and diff
* the valueA with that */
const value =
(isValueAArray && isArray(valueB)) || (isValueAPlainObject && isPlainObject(valueB))
? valueB
: isValueAArray ? [] : objectCreate(null);
const valueRecords = diff(valueA, value, options);
if (valueRecords.length) {
/* only add if there are changes */
patchRecords.push(
createPatchRecord(type, name, createValuePropertyDescriptor(value), diff(valueA, value, options))
);
}
} else if (isCustomDiff(valueA) && !isCustomDiff(valueB)) {
/* complex diff left hand */
const result = valueA.diff(valueB, name, b);
if (result) {
patchRecords.push(result);
}
} else if (isCustomDiff(valueB)) {
/* complex diff right hand */
const result = valueB.diff(valueA, name, a);
if (result) {
patchRecords.push(result);
}
} else if (
isPrimitive(valueA) ||
(allowFunctionValues && typeof valueA === 'function') ||
isIgnoredPropertyValue(name, a, b, ignorePropertyValues)
) {
/* primitive values, functions values if allowed, or ignored property values can just be copied */
patchRecords.push(createPatchRecord(type, name, createValuePropertyDescriptor(valueA)));
} else {
throw new TypeError(
`Value of property named "${name}" from first argument is not a primative, plain Object, or Array.`
);
}
return patchRecords;
}, patchRecords);
/* look for keys in b that are not in a */
keys(comparableB).reduce((patchRecords, name) => {
if (!hasOwnProperty.call(comparableA, name)) {
patchRecords.push(createPatchRecord('delete', name));
}
return patchRecords;
}, patchRecords);
return patchRecords;
}