2019-12-05 13:16:54 +01:00
|
|
|
const isUndefined = (o: any, key: string, nested?: string) => {
|
|
|
|
|
if (typeof o === "undefined" || typeof o[key] === "undefined") {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (nested) {
|
|
|
|
|
return typeof o[key][nested] === "undefined";
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const byKey = (key: string) => {
|
|
|
|
|
return (a: any, b: any) => {
|
|
|
|
|
if (isUndefined(a, key)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isUndefined(b, key)) {
|
2020-02-19 14:30:52 +01:00
|
|
|
return -1;
|
2019-12-05 13:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a[key] < b[key]) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (a[key] > b[key]) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const byValueLength = (key: string) => {
|
|
|
|
|
return (a: any, b: any) => {
|
|
|
|
|
if (isUndefined(a, key)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isUndefined(b, key)) {
|
2020-02-19 14:30:52 +01:00
|
|
|
return -1;
|
2019-12-05 13:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a[key].length < b[key].length) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (a[key].length > b[key].length) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const byNestedKeys = (key: string, nestedKey: string) => {
|
|
|
|
|
return (a: any, b: any) => {
|
|
|
|
|
if (isUndefined(a, key, nestedKey)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isUndefined(b, key, nestedKey)) {
|
2020-02-19 14:30:52 +01:00
|
|
|
return -1;
|
2019-12-05 13:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a[key][nestedKey] < b[key][nestedKey]) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (a[key][nestedKey] > b[key][nestedKey]) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
byKey,
|
|
|
|
|
byValueLength,
|
|
|
|
|
byNestedKeys
|
|
|
|
|
};
|