这个问题有点冗长,但是请忍受我。
我有一个用于.sort()
的通用价值比较功能:
export function compareObjectValues(key: string, direction: string = 'asc') {
// used as a comparator supplied to .sort() for sorting arrays of objects
return function(a: Object, b: Object): number {
const propertyA = getDescendantProperty(a, key);
const propertyB = getDescendantProperty(b, key);
if (!propertyA || !propertyB) {
return 0;
}
const normalizedPropA = (typeof propertyA === 'string') ? (propertyA as string).toLocaleLowerCase() : propertyA;
const normalizedPropB = (typeof propertyB === 'string') ? (propertyB as string).toLocaleLowerCase() : propertyB;
let comparison = 0;
if (normalizedPropA > normalizedPropB) {
comparison = 1;
}
else {
comparison = -1;
}
if (typeof normalizedPropA === 'number' && typeof normalizedPropB === 'number') {
console.log('comparison', comparison);
return comparison;
}
else {
console.log('string comparison', comparison);
return (direction === 'desc') ? (comparison * -1) : comparison;
}
};
}
内部帮助函数getDescendantProperty()
用于通过点路径安全地访问对象上的值:
export function getDescendantProperty(obj: object, path: string) {
// safely access nested properties if the property of the object is itself, an object
if (path.includes('.')) {
return path.split('.').reduce((accumulator: object, part: string) => accumulator && accumulator[part] || undefined, obj);
}
else {
return obj[path];
}
}
当我使用字符串对对象数组进行排序时,compareObjectValues()
会按预期工作。使用数字值时,它会失败,并且不会遍历所有期望值进行比较。
示例:
describe('on an object that uses a number for sorting', () => {
it('should return the comparison of the values in the correct order', () => {
const objects = [
{ id: 4 },
{ id: 0 },
{ id: 2 },
{ id: 3 },
{ id: 1 },
];
const sortedObjects = objects.sort(utils.compareObjectValues('id'));
expect(sortedObjects).toEqual([{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]);
});
});
业力输出:
LOG: 'comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 0 of 2 SUCCESS (0 secs / 0 secs)
LOG: 'comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 0 of 2 SUCCESS (0 secs / 0 secs)
LOG: 'comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 0 of 2 SUCCESS (0 secs / 0 secs)
Chrome 74.0.3729 (Mac OS X 10.14.5) Utils tests When calling `compareObjectValues` on an object that uses a number for sorting should return the comparison of the numbers in the correct order FAILED
Error: Expected $[0].id = 4 to equal 0.
Expected $[1].id = 0 to equal 1.
Expected $[2].id = 1 to equal 2.
Expected $[3].id = 2 to equal 3.
Expected $[4].id = 3 to equal 4.
at <Jasmine>
at UserContext.<anonymous> (src/app/lib/utils.spec.ts:133:31)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:391:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/zone-testing.js:308:1)
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
Chrome 74.0.3729 (Mac OS X 10.14.5) Utils tests When calling `compareObjectValues` on an object that uses a number for sorting should return the comparison of the numbers in the correct order FAILED
Error: Expected $[0].id = 4 to equal 0.
Expected $[1].id = 0 to equal 1.
Expected $[2].id = 1 to equal 2.
Expected $[3].id = 2 to equal 3.
Expected $[4].id = 3 to equal 4.
at <Jasmine>
at UserContext.<anonymous> (src/app/lib/utils.spec.ts:133:31)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:391:1)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 2 of 2 (1 FAILED) (0.19 secs / 0.16 secs)
TOTAL: 1 FAILED, 1 SUCCESS
TOTAL: 1 FAILED, 1 SUCCESS
npm ERR! Test failed. See above for more details.
从日志中可以看到,当使用数字值处理对象时,它在3次迭代后停止。使用字符串值,它处理了9。
我已验证getDescendantProperty()
返回了期望值。我还验证了如果通过在测试中将函数传递给compareObjectValues()
来手动应用相同的比较,则.sort()
的逻辑可以按预期工作。
我在隔离错误时遇到了一些困难,将不胜感激。
答案 0 :(得分:2)
问题在于对属性的这种懒惰检查:
if (!propertyA || !propertyB) return 0;
这对于检查某些类型的值效果很好,但在检查数字(!0 === true
),空字符串(!'' === true
)等时会给您带来意想不到的结果。
请使用严格检查:
if (typeof propertyA === 'undefined' || typeof propertyB === 'undefined') return 0;