add types for compare-func (#30630)

This commit is contained in:
Dogan Fennibay
2018-11-18 11:43:25 -08:00
committed by Pranav Senthilnathan
parent efb26511dd
commit 8225c94050
4 changed files with 66 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
import compareFunc = require("compare-func");
// This is taken from https://github.com/stevemao/compare-func/blob/master/README.md#usage and extended
// sort by an object property
[{ x: "b" }, { x: "a" }, { x: "c" }].sort(compareFunc("x"));
// => [{x: "a"}, {x: "b"}, {x: "c"}]
// sort by a nested object property
[{ x: { y: "b" } }, { x: { y: "a" } }].sort(compareFunc("x.y"));
// => [{x: {y: "a"}}, {x: {y: "b"}}]
// sort by the `x` property, then `y`
[{ x: "c", y: "c" }, { x: "b", y: "a" }, { x: "b", y: "b" }].sort(compareFunc(["x", "y"]));
// => [{x: "b", y: "a"}, {x: "b", y: "b"}, {x: "c", y: "c"}]
// sort by the returned value
[{ x: "b" }, { x: "a" }, { x: "c" }].sort(compareFunc((el) => el.x));
// => [{x: "a"}, {x: "b"}, {x: "c"}]
// sort by an array of functions
[{ x: "c", y: "c" }, { x: "b", y: "a" }, { x: "b", y: "b" }].sort(compareFunc([
(el) => el.x,
(el) => el.y,
]));
// => [{x: "a"}, {x: "b"}, {x: "c"}]
// sort by itself
[1, 3, 2].sort(compareFunc());
// => [1, 2, 3]
+12
View File
@@ -0,0 +1,12 @@
// Type definitions for compare-func 1.3
// Project: https://github.com/stevemao/compare-func
// Definitions by: Dogan Fennibay <https://github.com/fennibay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
type extractFunc<T> = (e: T) => number | string;
declare function compare_func<T>(prop?: ReadonlyArray<string | extractFunc<T>> | string | extractFunc<T>):
(e1: T, e2: T) => number;
export = compare_func;
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"compare-func-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }