當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript object.keys函數代碼示例

本文整理匯總了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 };
}
開發者ID:bryanforbes,項目名稱:core,代碼行數:32,代碼來源:compare.ts

示例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;
}
開發者ID:jason0x43,項目名稱:core,代碼行數:86,代碼來源:compare.ts


注:本文中的@dojo/shim/object.keys函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。