@fluid-experimental/property-common 0.49.0 → 0.50.0-41540
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +4 -0
- package/dist/constants.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/lib/chronometer.d.ts +55 -0
- package/lib/chronometer.d.ts.map +1 -0
- package/lib/chronometer.js +185 -0
- package/lib/chronometer.js.map +1 -0
- package/lib/consoleUtils.d.ts +16 -0
- package/lib/consoleUtils.d.ts.map +1 -0
- package/lib/consoleUtils.js +20 -0
- package/lib/consoleUtils.js.map +1 -0
- package/lib/constants.d.ts +1219 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +1274 -0
- package/lib/constants.js.map +1 -0
- package/lib/datastructures/collection.d.ts +203 -0
- package/lib/datastructures/collection.d.ts.map +1 -0
- package/lib/datastructures/collection.js +377 -0
- package/lib/datastructures/collection.js.map +1 -0
- package/lib/datastructures/dataArray.d.ts +275 -0
- package/lib/datastructures/dataArray.d.ts.map +1 -0
- package/lib/datastructures/dataArray.js +497 -0
- package/lib/datastructures/dataArray.js.map +1 -0
- package/lib/datastructures/index.d.ts +9 -0
- package/lib/datastructures/index.d.ts.map +1 -0
- package/lib/datastructures/index.js +9 -0
- package/lib/datastructures/index.js.map +1 -0
- package/lib/datastructures/integer64.d.ts +47 -0
- package/lib/datastructures/integer64.d.ts.map +1 -0
- package/lib/datastructures/integer64.js +140 -0
- package/lib/datastructures/integer64.js.map +1 -0
- package/lib/datastructures/sortedCollection.d.ts +77 -0
- package/lib/datastructures/sortedCollection.d.ts.map +1 -0
- package/lib/datastructures/sortedCollection.js +140 -0
- package/lib/datastructures/sortedCollection.js.map +1 -0
- package/lib/deferredPromise.d.ts +28 -0
- package/lib/deferredPromise.d.ts.map +1 -0
- package/lib/deferredPromise.js +43 -0
- package/lib/deferredPromise.js.map +1 -0
- package/lib/deterministicRandomGenerator.d.ts +39 -0
- package/lib/deterministicRandomGenerator.d.ts.map +1 -0
- package/lib/deterministicRandomGenerator.js +83 -0
- package/lib/deterministicRandomGenerator.js.map +1 -0
- package/lib/error_objects/flaggedError.d.ts +29 -0
- package/lib/error_objects/flaggedError.d.ts.map +1 -0
- package/lib/error_objects/flaggedError.js +45 -0
- package/lib/error_objects/flaggedError.js.map +1 -0
- package/lib/error_objects/httpError.d.ts +31 -0
- package/lib/error_objects/httpError.d.ts.map +1 -0
- package/lib/error_objects/httpError.js +58 -0
- package/lib/error_objects/httpError.js.map +1 -0
- package/lib/error_objects/httpErrorNoStack.d.ts +25 -0
- package/lib/error_objects/httpErrorNoStack.d.ts.map +1 -0
- package/lib/error_objects/httpErrorNoStack.js +29 -0
- package/lib/error_objects/httpErrorNoStack.js.map +1 -0
- package/lib/error_objects/index.d.ts +9 -0
- package/lib/error_objects/index.d.ts.map +1 -0
- package/lib/error_objects/index.js +9 -0
- package/lib/error_objects/index.js.map +1 -0
- package/lib/error_objects/operationError.d.ts +32 -0
- package/lib/error_objects/operationError.d.ts.map +1 -0
- package/lib/error_objects/operationError.js +73 -0
- package/lib/error_objects/operationError.js.map +1 -0
- package/lib/guidUtils.d.ts +16 -0
- package/lib/guidUtils.d.ts.map +1 -0
- package/lib/guidUtils.js +297 -0
- package/lib/guidUtils.js.map +1 -0
- package/lib/hashCalculator.d.ts +6 -0
- package/lib/hashCalculator.d.ts.map +1 -0
- package/lib/hashCalculator.js +14 -0
- package/lib/hashCalculator.js.map +1 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +16 -0
- package/lib/index.js.map +1 -0
- package/lib/joinPaths.d.ts +19 -0
- package/lib/joinPaths.d.ts.map +1 -0
- package/lib/joinPaths.js +28 -0
- package/lib/joinPaths.js.map +1 -0
- package/lib/packageVersion.d.ts +9 -0
- package/lib/packageVersion.d.ts.map +1 -0
- package/lib/packageVersion.js +9 -0
- package/lib/packageVersion.js.map +1 -0
- package/package.json +22 -2
- package/platform-dependent/package.json +1 -1
- package/src/constants.ts +5 -0
- package/src/packageVersion.ts +1 -1
- package/dist/test/chronometer.spec.js +0 -90
- package/dist/test/chronometer.spec.js.map +0 -1
- package/dist/test/datastructures/collection.spec.js +0 -401
- package/dist/test/datastructures/collection.spec.js.map +0 -1
- package/dist/test/datastructures/dataArray.spec.js +0 -79
- package/dist/test/datastructures/dataArray.spec.js.map +0 -1
- package/dist/test/datastructures/sortedCollection.spec.js +0 -106
- package/dist/test/datastructures/sortedCollection.spec.js.map +0 -1
- package/dist/test/deterministicRandomGenerator.spec.js +0 -18
- package/dist/test/deterministicRandomGenerator.spec.js.map +0 -1
- package/dist/test/error_objects/flaggedError.spec.js +0 -23
- package/dist/test/error_objects/flaggedError.spec.js.map +0 -1
- package/dist/test/error_objects/httpError.spec.js +0 -61
- package/dist/test/error_objects/httpError.spec.js.map +0 -1
- package/dist/test/error_objects/httpErrorNoStack.spec.js +0 -55
- package/dist/test/error_objects/httpErrorNoStack.spec.js.map +0 -1
- package/dist/test/error_objects/operationError.spec.js +0 -132
- package/dist/test/error_objects/operationError.spec.js.map +0 -1
- package/dist/test/guidUtils.spec.js +0 -184
- package/dist/test/guidUtils.spec.js.map +0 -1
- package/dist/test/strings/joinPaths.spec.js +0 -18
- package/dist/test/strings/joinPaths.spec.js.map +0 -1
- package/dist/test/tsconfig.tsbuildinfo +0 -2278
- package/nyc/mocha-junit-report.xml +0 -175
- package/src/test/chronometer.spec.ts +0 -99
- package/src/test/datastructures/collection.spec.ts +0 -567
- package/src/test/datastructures/dataArray.spec.ts +0 -81
- package/src/test/datastructures/sortedCollection.spec.ts +0 -128
- package/src/test/deterministicRandomGenerator.spec.ts +0 -18
- package/src/test/error_objects/flaggedError.spec.ts +0 -22
- package/src/test/error_objects/httpError.spec.ts +0 -68
- package/src/test/error_objects/httpErrorNoStack.spec.ts +0 -64
- package/src/test/error_objects/operationError.spec.ts +0 -152
- package/src/test/guidUtils.spec.ts +0 -213
- package/src/test/strings/joinPaths.spec.ts +0 -17
- package/src/test/tsconfig.json +0 -21
- package/tsconfig.esnext.tsbuildinfo +0 -824
- package/tsconfig.tsbuildinfo +0 -823
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @fileoverview implements (U)Int64 Datastructures
|
|
7
|
+
*/
|
|
8
|
+
import _ from "lodash";
|
|
9
|
+
import { ConsoleUtils } from "../consoleUtils";
|
|
10
|
+
import { constants } from "../constants";
|
|
11
|
+
const BIT32 = 4294967296;
|
|
12
|
+
const { MSG } = constants;
|
|
13
|
+
/**
|
|
14
|
+
* A data representation class for 64 bit integer types.
|
|
15
|
+
* This is necessary since js doesn't support 64bit
|
|
16
|
+
* integers natively yet.
|
|
17
|
+
*
|
|
18
|
+
* Please note this class is immutable.
|
|
19
|
+
* There are and there won't be set functions!
|
|
20
|
+
* (see javascript String class)
|
|
21
|
+
*
|
|
22
|
+
* @param low - lower 32 bit
|
|
23
|
+
* @param high - higher 32 bit
|
|
24
|
+
*/
|
|
25
|
+
export class Integer64 {
|
|
26
|
+
constructor(low = 0, high = 0) {
|
|
27
|
+
this.low = low;
|
|
28
|
+
this.high = high;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @returns the higher 32 bit integer part
|
|
32
|
+
*/
|
|
33
|
+
getValueHigh() {
|
|
34
|
+
return this.high;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @returns the lower 32 bit integer part
|
|
38
|
+
*/
|
|
39
|
+
getValueLow() {
|
|
40
|
+
return this.low;
|
|
41
|
+
}
|
|
42
|
+
_int64toString(isSigned, in_radix = 10) {
|
|
43
|
+
ConsoleUtils.assert(_.isNumber(in_radix), `${MSG.IN_RADIX_MUST_BE_NUMBER} ${in_radix}`);
|
|
44
|
+
ConsoleUtils.assert(in_radix >= 2 && 36 >= in_radix, `${MSG.BASE_OUT_OF_RANGE} ${in_radix}`);
|
|
45
|
+
let high = this.getValueHigh();
|
|
46
|
+
let low = this.getValueLow();
|
|
47
|
+
let result = "";
|
|
48
|
+
const sign = !!(isSigned && (high & 0x80000000)); // eslint-disable-line no-bitwise
|
|
49
|
+
if (sign) {
|
|
50
|
+
high = ~high; // eslint-disable-line no-bitwise
|
|
51
|
+
low = BIT32 - low;
|
|
52
|
+
}
|
|
53
|
+
do {
|
|
54
|
+
const mod = (high % in_radix) * BIT32 + low;
|
|
55
|
+
high = Math.floor(high / in_radix);
|
|
56
|
+
low = Math.floor(mod / in_radix);
|
|
57
|
+
result = (mod % in_radix).toString(in_radix) + result;
|
|
58
|
+
} while (high || low);
|
|
59
|
+
return sign ? `-${result}` : result;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* stringToInt function parses a string argument updates object's lower and higher 32 bit integer parts.
|
|
64
|
+
*
|
|
65
|
+
* @param in_signed - If the expect response should be signed or unsigned.
|
|
66
|
+
* @param in_string - The value to parse. Leading whitespace in the string argument is ignored.
|
|
67
|
+
* @param in_radix - An integer between 2 and 36 that represents the
|
|
68
|
+
* radix (the base in mathematical numeral systems) of the above mentioned string.
|
|
69
|
+
* @throws if in_string is not a string
|
|
70
|
+
* @throws if in_radix is entered but is not a number between 2 and 36
|
|
71
|
+
* @throws if the property is a Uint64 property and in_string is a negative number
|
|
72
|
+
* @throws if in_string contains characters other than numbers
|
|
73
|
+
* @returns low and high bits of Int64
|
|
74
|
+
*/
|
|
75
|
+
function _stringToInt64(in_signed, in_string, in_radix = 10) {
|
|
76
|
+
ConsoleUtils.assert(_.isString(in_string), MSG.IN_STRING_MUST_BE_STRING + in_string);
|
|
77
|
+
const string = in_string.trim();
|
|
78
|
+
ConsoleUtils.assert(_.isNumber(in_radix), `${MSG.IN_RADIX_BETWEEN_2_36} ${in_radix}`);
|
|
79
|
+
ConsoleUtils.assert(in_radix >= 2 && 36 >= in_radix, `${MSG.BASE_OUT_OF_RANGE} ${in_radix}`);
|
|
80
|
+
let position = 0;
|
|
81
|
+
let negative = false;
|
|
82
|
+
let high = 0;
|
|
83
|
+
let low = 0;
|
|
84
|
+
if (string[0] === "-") {
|
|
85
|
+
negative = true;
|
|
86
|
+
position += 1;
|
|
87
|
+
}
|
|
88
|
+
ConsoleUtils.assert(!negative || in_signed, MSG.CANNOT_UPDATE_TO_NEGATIVE + string);
|
|
89
|
+
while (position < string.length) {
|
|
90
|
+
const digit = parseInt(string[position++], in_radix);
|
|
91
|
+
if (isNaN(digit)) {
|
|
92
|
+
throw new Error(MSG.CANNOT_PARSE_INVALID_CHARACTERS + string);
|
|
93
|
+
}
|
|
94
|
+
low = low * in_radix + digit;
|
|
95
|
+
high = high * in_radix + Math.floor(low / BIT32);
|
|
96
|
+
low %= BIT32;
|
|
97
|
+
}
|
|
98
|
+
if (negative) {
|
|
99
|
+
// eslint-disable-next-line no-bitwise
|
|
100
|
+
high = ~high;
|
|
101
|
+
if (low) {
|
|
102
|
+
low = BIT32 - low;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
high += 1;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return [low, high];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* A data representation class for the signed 64 bit integer type
|
|
112
|
+
*/
|
|
113
|
+
export class Int64 extends Integer64 {
|
|
114
|
+
clone() {
|
|
115
|
+
return new Int64(this.low, this.high);
|
|
116
|
+
}
|
|
117
|
+
toString(radix = 10) {
|
|
118
|
+
return this._int64toString(true, radix);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
Int64.fromString = function (in_string, radix = 10) {
|
|
122
|
+
const [low, high] = _stringToInt64(true, in_string, radix);
|
|
123
|
+
return new Int64(low, high);
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* A data representation class for the unsigned 64 bit integer type
|
|
127
|
+
*/
|
|
128
|
+
export class Uint64 extends Integer64 {
|
|
129
|
+
static fromString(in_string, in_radix = 10) {
|
|
130
|
+
const [low, high] = _stringToInt64.call(this, false, in_string, in_radix);
|
|
131
|
+
return new Uint64(low, high);
|
|
132
|
+
}
|
|
133
|
+
clone() {
|
|
134
|
+
return new Uint64(this.low, this.high);
|
|
135
|
+
}
|
|
136
|
+
toString(radix) {
|
|
137
|
+
return this._int64toString(false, radix);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=integer64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"integer64.js","sourceRoot":"","sources":["../../src/datastructures/integer64.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AAEH,OAAO,CAAC,MAAM,QAAQ,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,MAAM,KAAK,GAAG,UAAU,CAAC;AACzB,MAAM,EAAE,GAAG,EAAE,GAAG,SAAS,CAAC;AAC1B;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,SAAS;IAClB,YAAsB,MAAM,CAAC,EAAY,OAAO,CAAC;QAA3B,QAAG,GAAH,GAAG,CAAI;QAAY,SAAI,GAAJ,IAAI,CAAI;IACjD,CAAC;IAED;;OAEG;IACH,YAAY;QACR,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,WAAW;QACP,OAAO,IAAI,CAAC,GAAG,CAAC;IACpB,CAAC;IAES,cAAc,CAAC,QAAiB,EAAE,QAAQ,GAAG,EAAE;QACrD,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,GAAG,GAAG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AAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * @fileoverview implements (U)Int64 Datastructures\n */\n\nimport _ from \"lodash\";\nimport { ConsoleUtils } from \"../consoleUtils\";\nimport { constants } from \"../constants\";\n\nconst BIT32 = 4294967296;\nconst { MSG } = constants;\n/**\n * A data representation class for 64 bit integer types.\n * This is necessary since js doesn't support 64bit\n * integers natively yet.\n *\n * Please note this class is immutable.\n * There are and there won't be set functions!\n * (see javascript String class)\n *\n * @param low - lower 32 bit\n * @param high - higher 32 bit\n */\nexport class Integer64 {\n constructor(protected low = 0, protected high = 0) {\n }\n\n /**\n * @returns the higher 32 bit integer part\n */\n getValueHigh() {\n return this.high;\n }\n\n /**\n * @returns the lower 32 bit integer part\n */\n getValueLow() {\n return this.low;\n }\n\n protected _int64toString(isSigned: boolean, in_radix = 10): string {\n ConsoleUtils.assert(_.isNumber(in_radix), `${MSG.IN_RADIX_MUST_BE_NUMBER} ${in_radix}`);\n ConsoleUtils.assert(in_radix >= 2 && 36 >= in_radix, `${MSG.BASE_OUT_OF_RANGE} ${in_radix}`);\n\n let high = this.getValueHigh();\n let low = this.getValueLow();\n let result = \"\";\n const sign = !!(isSigned && (high & 0x80000000)); // eslint-disable-line no-bitwise\n if (sign) {\n high = ~high; // eslint-disable-line no-bitwise\n low = BIT32 - low;\n }\n do {\n const mod = (high % in_radix) * BIT32 + low;\n high = Math.floor(high / in_radix);\n low = Math.floor(mod / in_radix);\n result = (mod % in_radix).toString(in_radix) + result;\n } while (high || low);\n\n return sign ? `-${result}` : result;\n }\n}\n\n/**\n * stringToInt function parses a string argument updates object's lower and higher 32 bit integer parts.\n *\n * @param in_signed - If the expect response should be signed or unsigned.\n * @param in_string - The value to parse. Leading whitespace in the string argument is ignored.\n * @param in_radix - An integer between 2 and 36 that represents the\n * radix (the base in mathematical numeral systems) of the above mentioned string.\n * @throws if in_string is not a string\n * @throws if in_radix is entered but is not a number between 2 and 36\n * @throws if the property is a Uint64 property and in_string is a negative number\n * @throws if in_string contains characters other than numbers\n * @returns low and high bits of Int64\n */\nfunction _stringToInt64(in_signed: boolean, in_string: string, in_radix = 10): number[] {\n ConsoleUtils.assert(_.isString(in_string), MSG.IN_STRING_MUST_BE_STRING + in_string);\n const string = in_string.trim();\n\n ConsoleUtils.assert(_.isNumber(in_radix), `${MSG.IN_RADIX_BETWEEN_2_36} ${in_radix}`);\n ConsoleUtils.assert(in_radix >= 2 && 36 >= in_radix, `${MSG.BASE_OUT_OF_RANGE} ${in_radix}`);\n\n let position = 0;\n let negative = false;\n let high = 0;\n let low = 0;\n if (string[0] === \"-\") {\n negative = true;\n position += 1;\n }\n\n ConsoleUtils.assert(!negative || in_signed, MSG.CANNOT_UPDATE_TO_NEGATIVE + string);\n\n while (position < string.length) {\n const digit = parseInt(string[position++], in_radix);\n if (isNaN(digit)) {\n throw new Error(MSG.CANNOT_PARSE_INVALID_CHARACTERS + string);\n }\n low = low * in_radix + digit;\n high = high * in_radix + Math.floor(low / BIT32);\n low %= BIT32;\n }\n\n if (negative) {\n // eslint-disable-next-line no-bitwise\n high = ~high;\n if (low) {\n low = BIT32 - low;\n } else {\n high += 1;\n }\n }\n\n return [low, high];\n}\n\n/**\n * A data representation class for the signed 64 bit integer type\n */\nexport class Int64 extends Integer64 {\n static fromString = function(in_string: string, radix = 10) {\n const [low, high] = _stringToInt64(true, in_string, radix);\n return new Int64(low, high);\n };\n\n clone() {\n return new Int64(this.low, this.high);\n }\n\n toString(radix = 10) {\n return this._int64toString(true, radix);\n }\n}\n\n/**\n * A data representation class for the unsigned 64 bit integer type\n */\nexport class Uint64 extends Integer64 {\n static fromString(in_string: string, in_radix = 10) {\n const [low, high] = _stringToInt64.call(this, false, in_string, in_radix);\n return new Uint64(low, high);\n }\n\n clone() {\n return new Uint64(this.low, this.high);\n }\n\n toString(radix) {\n return this._int64toString(false, radix);\n }\n}\n"]}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { Collection } from "./collection";
|
|
6
|
+
export declare class SortedCollection<T> extends Collection<T> {
|
|
7
|
+
private _sortedKeys;
|
|
8
|
+
private _comparisonFunction?;
|
|
9
|
+
/**
|
|
10
|
+
* A sorted collection class.
|
|
11
|
+
* @param in_name - a friendly name to describe this collection. If undefined
|
|
12
|
+
* the collection will have a default "Untitled Collection" assigned to its name.
|
|
13
|
+
* @param in_type - optional parameter pointing to the constructor
|
|
14
|
+
* of a type this Collection will host.
|
|
15
|
+
*/
|
|
16
|
+
constructor(in_name?: string, in_type?: any);
|
|
17
|
+
/**
|
|
18
|
+
* Set the comparison function. By default the keys will be sorted wrt their ASCII code.
|
|
19
|
+
* @param in_fn - The function to compare two entries.
|
|
20
|
+
* the return value of this function must convey to the following cases:
|
|
21
|
+
* - if a \> b, then the return value must be greater than 0
|
|
22
|
+
* - if a \< b, then the return value must be less than 0
|
|
23
|
+
* - if a == b, then the return value must be 0
|
|
24
|
+
*/
|
|
25
|
+
setComparisonFunction(in_fn?: (x: string, y: string) => number): void;
|
|
26
|
+
/**
|
|
27
|
+
* Add an item to the collection. Sort the list of keys in an ascending order.
|
|
28
|
+
* @param in_key - Key to store the value under
|
|
29
|
+
* @param in_value - Value to store in the collection
|
|
30
|
+
* @returns Return the value passed in
|
|
31
|
+
*/
|
|
32
|
+
add(in_key: string | number, in_value: T): T;
|
|
33
|
+
/**
|
|
34
|
+
* Remove an item from this Collection. This method returns a Boolean indicating
|
|
35
|
+
* the success or failure of the removal. This is practical because if we were
|
|
36
|
+
* to throw an error when the key doesn't exist, it would require additional
|
|
37
|
+
* checks by the caller to make sure this key exists prior to removal. Which
|
|
38
|
+
* would make the attempt of removal more verbose and also costly because the
|
|
39
|
+
* caller would have to keep a list – somewhere else – of the things he can
|
|
40
|
+
* and cannot remove.
|
|
41
|
+
*
|
|
42
|
+
* @param in_key - the key we wish to remove
|
|
43
|
+
* @returns true if the key exists and was removed, false otherwise.
|
|
44
|
+
*/
|
|
45
|
+
remove(in_key: string): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Copy the items of in_collection to this collection.
|
|
48
|
+
* @returns cloned SortedCollection
|
|
49
|
+
*/
|
|
50
|
+
clone(): SortedCollection<T>;
|
|
51
|
+
/**
|
|
52
|
+
* Internal function use to search (binary search) for the nearest index that
|
|
53
|
+
* the given key would be inserted.
|
|
54
|
+
* i.e. given the array [10, 20, 30, 40, 50] the index that 35 should be inserted at is 3
|
|
55
|
+
* @param in_array - Target array
|
|
56
|
+
* @param in_key - Key to check against
|
|
57
|
+
* @returns The index at which the key would be inserted in
|
|
58
|
+
*/
|
|
59
|
+
private _binarySearchNearestIndex;
|
|
60
|
+
/**
|
|
61
|
+
* Return the nearest next item to the given key i.e.
|
|
62
|
+
* For the given list of keys ['1.0.1', '2.0.0', '2.2.0', '7.0.1'] the nearest next item to 6.0.1 is
|
|
63
|
+
* the item mapped by '7.0.1'
|
|
64
|
+
* @param in_key - The key to check against in order to get the nearest next item
|
|
65
|
+
* @returns The nearest next item
|
|
66
|
+
*/
|
|
67
|
+
getNearestNextItem(in_key: string): T | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Return the nearest previous item to the given key i.e.
|
|
70
|
+
* For the given list of keys ['1.0.1', '2.0.0', '2.2.0', '7.0.1'] the nearest previous item to 6.0.1 is
|
|
71
|
+
* the item mapped by '7.0.1'
|
|
72
|
+
* @param in_key - The key to check against in order to get the nearest previous item
|
|
73
|
+
* @returns The nearest previous item
|
|
74
|
+
*/
|
|
75
|
+
getNearestPreviousItem(in_key: string | number): T | undefined;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=sortedCollection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortedCollection.d.ts","sourceRoot":"","sources":["../../src/datastructures/sortedCollection.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,qBAAa,gBAAgB,CAAC,CAAC,CAAE,SAAQ,UAAU,CAAC,CAAC,CAAC;IAClD,OAAO,CAAC,WAAW,CAAgB;IACnC,OAAO,CAAC,mBAAmB,CAAC,CAA+C;IAE3E;;;;;;OAMG;gBACS,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,KAAA;IAItC;;;;;;;OAOG;IACH,qBAAqB,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM;IAK9D;;;;;OAKG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,CAAC;IASxC;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM;IAMrB;;;OAGG;IACH,KAAK,IAAI,gBAAgB,CAAC,CAAC,CAAC;IAO5B;;;;;;;OAOG;IACH,OAAO,CAAC,yBAAyB;IAiCjC;;;;;;OAMG;IACH,kBAAkB,CAAC,MAAM,EAAE,MAAM;IASjC;;;;;;OAMG;IACH,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;CAQjD"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @fileoverview
|
|
7
|
+
* Declaration of the SortedCollection class
|
|
8
|
+
*/
|
|
9
|
+
import _ from "lodash";
|
|
10
|
+
import { Collection } from "./collection";
|
|
11
|
+
export class SortedCollection extends Collection {
|
|
12
|
+
/**
|
|
13
|
+
* A sorted collection class.
|
|
14
|
+
* @param in_name - a friendly name to describe this collection. If undefined
|
|
15
|
+
* the collection will have a default "Untitled Collection" assigned to its name.
|
|
16
|
+
* @param in_type - optional parameter pointing to the constructor
|
|
17
|
+
* of a type this Collection will host.
|
|
18
|
+
*/
|
|
19
|
+
constructor(in_name, in_type) {
|
|
20
|
+
super(in_name, in_type);
|
|
21
|
+
this._sortedKeys = [];
|
|
22
|
+
this._comparisonFunction = undefined;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Set the comparison function. By default the keys will be sorted wrt their ASCII code.
|
|
26
|
+
* @param in_fn - The function to compare two entries.
|
|
27
|
+
* the return value of this function must convey to the following cases:
|
|
28
|
+
* - if a \> b, then the return value must be greater than 0
|
|
29
|
+
* - if a \< b, then the return value must be less than 0
|
|
30
|
+
* - if a == b, then the return value must be 0
|
|
31
|
+
*/
|
|
32
|
+
setComparisonFunction(in_fn) {
|
|
33
|
+
console.assert(_.isFunction(in_fn), "Must provide a function");
|
|
34
|
+
this._comparisonFunction = in_fn;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Add an item to the collection. Sort the list of keys in an ascending order.
|
|
38
|
+
* @param in_key - Key to store the value under
|
|
39
|
+
* @param in_value - Value to store in the collection
|
|
40
|
+
* @returns Return the value passed in
|
|
41
|
+
*/
|
|
42
|
+
add(in_key, in_value) {
|
|
43
|
+
const toReturn = super.add(in_key, in_value);
|
|
44
|
+
this._sortedKeys.push(in_key.toString());
|
|
45
|
+
this._sortedKeys.sort(this._comparisonFunction);
|
|
46
|
+
return toReturn;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Remove an item from this Collection. This method returns a Boolean indicating
|
|
50
|
+
* the success or failure of the removal. This is practical because if we were
|
|
51
|
+
* to throw an error when the key doesn't exist, it would require additional
|
|
52
|
+
* checks by the caller to make sure this key exists prior to removal. Which
|
|
53
|
+
* would make the attempt of removal more verbose and also costly because the
|
|
54
|
+
* caller would have to keep a list – somewhere else – of the things he can
|
|
55
|
+
* and cannot remove.
|
|
56
|
+
*
|
|
57
|
+
* @param in_key - the key we wish to remove
|
|
58
|
+
* @returns true if the key exists and was removed, false otherwise.
|
|
59
|
+
*/
|
|
60
|
+
remove(in_key) {
|
|
61
|
+
const toReturn = super.remove(in_key);
|
|
62
|
+
this._sortedKeys = _.without(this._sortedKeys, in_key);
|
|
63
|
+
return toReturn;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Copy the items of in_collection to this collection.
|
|
67
|
+
* @returns cloned SortedCollection
|
|
68
|
+
*/
|
|
69
|
+
clone() {
|
|
70
|
+
const newCol = new SortedCollection(this._name, this._type);
|
|
71
|
+
newCol.setComparisonFunction(this._comparisonFunction);
|
|
72
|
+
newCol.bulkAdd(this._items);
|
|
73
|
+
return newCol;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Internal function use to search (binary search) for the nearest index that
|
|
77
|
+
* the given key would be inserted.
|
|
78
|
+
* i.e. given the array [10, 20, 30, 40, 50] the index that 35 should be inserted at is 3
|
|
79
|
+
* @param in_array - Target array
|
|
80
|
+
* @param in_key - Key to check against
|
|
81
|
+
* @returns The index at which the key would be inserted in
|
|
82
|
+
*/
|
|
83
|
+
_binarySearchNearestIndex(in_array, in_key) {
|
|
84
|
+
if (this._comparisonFunction === undefined) {
|
|
85
|
+
return _.sortedIndex(in_array, in_key);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const middleIndex = Math.floor((in_array.length - 1) / 2);
|
|
89
|
+
if (middleIndex < 0) {
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
if (this._comparisonFunction(in_array[middleIndex], in_key) > 0) {
|
|
93
|
+
if (in_array.length === 1) {
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
96
|
+
return this._binarySearchNearestIndex(in_array.slice(0, middleIndex), in_key);
|
|
97
|
+
}
|
|
98
|
+
if (this._comparisonFunction(in_array[middleIndex], in_key) < 0) {
|
|
99
|
+
if (in_array.length === 1) {
|
|
100
|
+
return 1;
|
|
101
|
+
}
|
|
102
|
+
return (middleIndex + 1) + this._binarySearchNearestIndex(in_array.slice(middleIndex + 1, in_array.length), in_key);
|
|
103
|
+
}
|
|
104
|
+
return middleIndex;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Return the nearest next item to the given key i.e.
|
|
109
|
+
* For the given list of keys ['1.0.1', '2.0.0', '2.2.0', '7.0.1'] the nearest next item to 6.0.1 is
|
|
110
|
+
* the item mapped by '7.0.1'
|
|
111
|
+
* @param in_key - The key to check against in order to get the nearest next item
|
|
112
|
+
* @returns The nearest next item
|
|
113
|
+
*/
|
|
114
|
+
getNearestNextItem(in_key) {
|
|
115
|
+
const closestNextIndex = this._binarySearchNearestIndex(this._sortedKeys, in_key.toString());
|
|
116
|
+
if (closestNextIndex === this.getCount()) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
return this.item(this._sortedKeys[closestNextIndex]);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Return the nearest previous item to the given key i.e.
|
|
125
|
+
* For the given list of keys ['1.0.1', '2.0.0', '2.2.0', '7.0.1'] the nearest previous item to 6.0.1 is
|
|
126
|
+
* the item mapped by '7.0.1'
|
|
127
|
+
* @param in_key - The key to check against in order to get the nearest previous item
|
|
128
|
+
* @returns The nearest previous item
|
|
129
|
+
*/
|
|
130
|
+
getNearestPreviousItem(in_key) {
|
|
131
|
+
const closestPreviousIndex = this._binarySearchNearestIndex(this._sortedKeys, in_key.toString());
|
|
132
|
+
if (closestPreviousIndex === 0) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
return this.item(this._sortedKeys[closestPreviousIndex - 1]);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=sortedCollection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortedCollection.js","sourceRoot":"","sources":["../../src/datastructures/sortedColl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ACtB;IACL,CAAC;IAED;;;;;;OAMG;IACH,kBAAkB,CAAC,MAAc;QAC7B,MAAM,gBAAgB,GAAG,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC7F,IAAI,gBAAgB,KAAK,IAAI,CAAC,QAAQ,EAAE,EAAE;YACtC,OAAO,SAAS,CAAC;SACpB;aAAM;YACH,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC,CAAC;SACxD;IACL,CAAC;IAED;;;;;;OAMG;IACH,sBAAsB,CAAC,MAAuB;QAC1C,MAAM,oBAAoB,GAAG,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;QACjG,IAAI,oBAAoB,KAAK,CAAC,EAAE;YAC5B,OAAO,SAAS,CAAC;SACpB;aAAM;YACH,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,oBAAoB,GAAG,CAAC,CAAC,CAAC,CAAC;SAChE;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * @fileoverview\n * Declaration of the SortedCollection class\n */\n\nimport _ from \"lodash\";\nimport { Collection } from \"./collection\";\n\nexport class SortedCollection<T> extends Collection<T> {\n private _sortedKeys: string[] = [];\n private _comparisonFunction?: (x: string, y: string) => number = undefined;\n\n /**\n * A sorted collection class.\n * @param in_name - a friendly name to describe this collection. If undefined\n * the collection will have a default \"Untitled Collection\" assigned to its name.\n * @param in_type - optional parameter pointing to the constructor\n * of a type this Collection will host.\n */\n constructor(in_name?: string, in_type?) {\n super(in_name, in_type);\n }\n\n /**\n * Set the comparison function. By default the keys will be sorted wrt their ASCII code.\n * @param in_fn - The function to compare two entries.\n * the return value of this function must convey to the following cases:\n * - if a \\> b, then the return value must be greater than 0\n * - if a \\< b, then the return value must be less than 0\n * - if a == b, then the return value must be 0\n */\n setComparisonFunction(in_fn?: (x: string, y: string) => number) {\n console.assert(_.isFunction(in_fn), \"Must provide a function\");\n this._comparisonFunction = in_fn;\n }\n\n /**\n * Add an item to the collection. Sort the list of keys in an ascending order.\n * @param in_key - Key to store the value under\n * @param in_value - Value to store in the collection\n * @returns Return the value passed in\n */\n add(in_key: string | number, in_value: T) {\n const toReturn = super.add(in_key, in_value);\n\n this._sortedKeys.push(in_key.toString());\n this._sortedKeys.sort(this._comparisonFunction);\n\n return toReturn;\n }\n\n /**\n * Remove an item from this Collection. This method returns a Boolean indicating\n * the success or failure of the removal. This is practical because if we were\n * to throw an error when the key doesn't exist, it would require additional\n * checks by the caller to make sure this key exists prior to removal. Which\n * would make the attempt of removal more verbose and also costly because the\n * caller would have to keep a list – somewhere else – of the things he can\n * and cannot remove.\n *\n * @param in_key - the key we wish to remove\n * @returns true if the key exists and was removed, false otherwise.\n */\n remove(in_key: string) {\n const toReturn = super.remove(in_key);\n this._sortedKeys = _.without(this._sortedKeys, in_key);\n return toReturn;\n }\n\n /**\n * Copy the items of in_collection to this collection.\n * @returns cloned SortedCollection\n */\n clone(): SortedCollection<T> {\n const newCol = new SortedCollection<T>(this._name, this._type);\n newCol.setComparisonFunction(this._comparisonFunction);\n newCol.bulkAdd(this._items);\n return newCol;\n }\n\n /**\n * Internal function use to search (binary search) for the nearest index that\n * the given key would be inserted.\n * i.e. given the array [10, 20, 30, 40, 50] the index that 35 should be inserted at is 3\n * @param in_array - Target array\n * @param in_key - Key to check against\n * @returns The index at which the key would be inserted in\n */\n private _binarySearchNearestIndex(in_array: string[], in_key: string): number {\n if (this._comparisonFunction === undefined) {\n return _.sortedIndex(in_array, in_key);\n } else {\n const middleIndex = Math.floor((in_array.length - 1) / 2);\n\n if (middleIndex < 0) {\n return 0;\n }\n\n if (this._comparisonFunction(in_array[middleIndex], in_key) > 0) {\n if (in_array.length === 1) {\n return 0;\n }\n\n return this._binarySearchNearestIndex(in_array.slice(0, middleIndex), in_key);\n }\n\n if (this._comparisonFunction(in_array[middleIndex], in_key) < 0) {\n if (in_array.length === 1) {\n return 1;\n }\n\n return (middleIndex + 1) + this._binarySearchNearestIndex(\n in_array.slice(middleIndex + 1, in_array.length),\n in_key,\n );\n }\n\n return middleIndex;\n }\n }\n\n /**\n * Return the nearest next item to the given key i.e.\n * For the given list of keys ['1.0.1', '2.0.0', '2.2.0', '7.0.1'] the nearest next item to 6.0.1 is\n * the item mapped by '7.0.1'\n * @param in_key - The key to check against in order to get the nearest next item\n * @returns The nearest next item\n */\n getNearestNextItem(in_key: string) {\n const closestNextIndex = this._binarySearchNearestIndex(this._sortedKeys, in_key.toString());\n if (closestNextIndex === this.getCount()) {\n return undefined;\n } else {\n return this.item(this._sortedKeys[closestNextIndex]);\n }\n }\n\n /**\n * Return the nearest previous item to the given key i.e.\n * For the given list of keys ['1.0.1', '2.0.0', '2.2.0', '7.0.1'] the nearest previous item to 6.0.1 is\n * the item mapped by '7.0.1'\n * @param in_key - The key to check against in order to get the nearest previous item\n * @returns The nearest previous item\n */\n getNearestPreviousItem(in_key: string | number) {\n const closestPreviousIndex = this._binarySearchNearestIndex(this._sortedKeys, in_key.toString());\n if (closestPreviousIndex === 0) {\n return undefined;\n } else {\n return this.item(this._sortedKeys[closestPreviousIndex - 1]);\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Promise that can be fulfilled or rejected later in an arbitrary manner (rather than
|
|
7
|
+
* through the constructor's executor).
|
|
8
|
+
* For example, a deferred promise could be fulfilled after waiting for many asynchronous
|
|
9
|
+
* tasks to terminate. This class becomes useful when combining classic async calls with promises.
|
|
10
|
+
*/
|
|
11
|
+
export declare class DeferredPromise<T> implements Promise<T> {
|
|
12
|
+
private _resolveSelf;
|
|
13
|
+
private _rejectSelf;
|
|
14
|
+
private readonly promise;
|
|
15
|
+
constructor();
|
|
16
|
+
[Symbol.toStringTag]: string;
|
|
17
|
+
finally(onfinally?: () => void): Promise<T>;
|
|
18
|
+
/**
|
|
19
|
+
* Fetches a node style callback that fulfills the promise when called.
|
|
20
|
+
* @returns A node style callback that fulfills the promise when called.
|
|
21
|
+
*/
|
|
22
|
+
getCb(): (error: any, result: any) => void;
|
|
23
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
|
|
24
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
|
|
25
|
+
resolve(val: T): void;
|
|
26
|
+
reject(reason: any): void;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=deferredPromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deferredPromise.d.ts","sourceRoot":"","sources":["../src/deferredPromise.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,qBAAa,eAAe,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IACjD,OAAO,CAAC,YAAY,CAAC;IACrB,OAAO,CAAC,WAAW,CAAC;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAa;;IAQrC,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAEhB,OAAO,CAAC,SAAS,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC;IAIxD;;;OAGG;IACH,KAAK;IAUQ,IAAI,CAAC,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,KAAK,EAC5C,WAAW,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,KACpB,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,EACxD,UAAU,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KACtB,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,GACzD,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAIlB,KAAK,CAAC,OAAO,GAAG,KAAK,EAC9B,UAAU,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KACtB,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,GACvD,OAAO,CAAC,CAAC,GAAG,OAAO,CAAC;IAIhB,OAAO,CAAC,GAAG,EAAE,CAAC;IACd,MAAM,CAAC,MAAM,EAAE,GAAG;CAC5B"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Promise that can be fulfilled or rejected later in an arbitrary manner (rather than
|
|
7
|
+
* through the constructor's executor).
|
|
8
|
+
* For example, a deferred promise could be fulfilled after waiting for many asynchronous
|
|
9
|
+
* tasks to terminate. This class becomes useful when combining classic async calls with promises.
|
|
10
|
+
*/
|
|
11
|
+
export class DeferredPromise {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.promise = new Promise((resolve, reject) => {
|
|
14
|
+
this._resolveSelf = resolve;
|
|
15
|
+
this._rejectSelf = reject;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async finally(onfinally) {
|
|
19
|
+
throw new Error("Method not implemented.");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Fetches a node style callback that fulfills the promise when called.
|
|
23
|
+
* @returns A node style callback that fulfills the promise when called.
|
|
24
|
+
*/
|
|
25
|
+
getCb() {
|
|
26
|
+
return (error, result) => {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
28
|
+
if (error) {
|
|
29
|
+
return this.reject(error);
|
|
30
|
+
}
|
|
31
|
+
return this.resolve(result);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async then(onfulfilled, onrejected) {
|
|
35
|
+
return this.promise.then(onfulfilled, onrejected);
|
|
36
|
+
}
|
|
37
|
+
async catch(onrejected) {
|
|
38
|
+
return this.promise.then(onrejected);
|
|
39
|
+
}
|
|
40
|
+
resolve(val) { this._resolveSelf(val); }
|
|
41
|
+
reject(reason) { this._rejectSelf(reason); }
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=deferredPromise.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deferredPromise.js","sourceRoot":"","sources":["../src/deferredPromise.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,MAAM,OAAO,eAAe;IAKxB;QACI,IAAI,CAAC,OAAO,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC;YAC5B,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC;QAC9B,CAAC,CAAC,CAAC;IACP,CAAC;IAGM,KAAK,CAAC,OAAO,CAAC,SAAsB;QACvC,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC/C,CAAC;IAED;;;OAGG;IACH,KAAK;QACD,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;YACrB,yEAAyE;YACzE,IAAI,KAAK,EAAE;gBACP,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aAC7B;YACD,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAChC,CAAC,CAAC;IACN,CAAC;IAEM,KAAK,CAAC,IAAI,CACb,WACwD,EACxD,UACwD;QAExD,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACtD,CAAC;IAEM,KAAK,CAAC,KAAK,CACd,UACsD;QAEtD,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACzC,CAAC;IAEM,OAAO,CAAC,GAAM,IAAI,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAC3C,MAAM,CAAC,MAAW,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;CAC3D","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Creates a Promise that can be fulfilled or rejected later in an arbitrary manner (rather than\n * through the constructor's executor).\n * For example, a deferred promise could be fulfilled after waiting for many asynchronous\n * tasks to terminate. This class becomes useful when combining classic async calls with promises.\n */\nexport class DeferredPromise<T> implements Promise<T> {\n private _resolveSelf;\n private _rejectSelf;\n private readonly promise: Promise<T>;\n\n constructor() {\n this.promise = new Promise((resolve, reject) => {\n this._resolveSelf = resolve;\n this._rejectSelf = reject;\n });\n }\n [Symbol.toStringTag]: string;\n\n public async finally(onfinally?: () => void): Promise<T> {\n throw new Error(\"Method not implemented.\");\n }\n\n /**\n * Fetches a node style callback that fulfills the promise when called.\n * @returns A node style callback that fulfills the promise when called.\n */\n getCb() {\n return (error, result) => {\n // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions\n if (error) {\n return this.reject(error);\n }\n return this.resolve(result);\n };\n }\n\n public async then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) =>\n TResult1 | PromiseLike<TResult1>) | undefined | null,\n onrejected?: ((reason: any) =>\n TResult2 | PromiseLike<TResult2>) | undefined | null,\n ): Promise<TResult1 | TResult2> {\n return this.promise.then(onfulfilled, onrejected);\n }\n\n public async catch<TResult = never>(\n onrejected?: ((reason: any) =>\n TResult | PromiseLike<TResult>) | undefined | null,\n ): Promise<T | TResult> {\n return this.promise.then(onrejected);\n }\n\n public resolve(val: T) { this._resolveSelf(val); }\n public reject(reason: any) { this._rejectSelf(reason); }\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Random number generator that creates a deterministic sequence of random numbers based on an initial seed GUID
|
|
7
|
+
*
|
|
8
|
+
* Warning: This is a very straight forward implementation based on the hashCombine4xUint32 function. It probably
|
|
9
|
+
* doesn't produce very high quality random numbers (do not use this for cryptography!) and it is not very
|
|
10
|
+
* efficient.
|
|
11
|
+
*/
|
|
12
|
+
export declare class DeterministicRandomGenerator {
|
|
13
|
+
_guid1: Uint32Array;
|
|
14
|
+
_guid2: Uint32Array;
|
|
15
|
+
_result: Uint32Array;
|
|
16
|
+
/**
|
|
17
|
+
* @param in_seed - The initial seed (it can be either a GUID or a number)
|
|
18
|
+
* which is used to initialize the random number generator
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
constructor(in_seed: string | number);
|
|
22
|
+
/**
|
|
23
|
+
* Creates a floating point random number
|
|
24
|
+
*
|
|
25
|
+
* @param in_max - If supplied the returned number will be 0 \<= number \< in_max. If none is given
|
|
26
|
+
* in_max = 1 is assumed
|
|
27
|
+
* @returns The random number
|
|
28
|
+
*/
|
|
29
|
+
random(in_max?: number): number;
|
|
30
|
+
/**
|
|
31
|
+
* Creates an integer point random number
|
|
32
|
+
*
|
|
33
|
+
* @param in_max - If supplied the returned number will be 0 \<= number \< in_max. If none is given
|
|
34
|
+
* in_max = 14294967296 (2^32) is assumed
|
|
35
|
+
* @returns The random number
|
|
36
|
+
*/
|
|
37
|
+
irandom(in_max?: number): number;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=deterministicRandomGenerator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deterministicRandomGenerator.d.ts","sourceRoot":"","sources":["../src/deterministicRandomGenerator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAYH;;;;;;GAMG;AACH,qBAAa,4BAA4B;IACrC,MAAM,EAAE,WAAW,CAAC;IACpB,MAAM,EAAE,WAAW,CAAC;IACpB,OAAO,EAAE,WAAW,CAAC;IAErB;;;;OAIG;gBACS,OAAO,EAAE,MAAM,GAAG,MAAM;IAgBpC;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,SAAM;IAKnB;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM;CAwBnC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @fileoverview Helper class to create a deterministic sequence of random numbers.
|
|
7
|
+
*/
|
|
8
|
+
/* eslint-disable no-bitwise */
|
|
9
|
+
import _ from "lodash";
|
|
10
|
+
import { GuidUtils } from "./guidUtils";
|
|
11
|
+
import { calculateHash } from "./hashCalculator";
|
|
12
|
+
/**
|
|
13
|
+
* Random number generator that creates a deterministic sequence of random numbers based on an initial seed GUID
|
|
14
|
+
*
|
|
15
|
+
* Warning: This is a very straight forward implementation based on the hashCombine4xUint32 function. It probably
|
|
16
|
+
* doesn't produce very high quality random numbers (do not use this for cryptography!) and it is not very
|
|
17
|
+
* efficient.
|
|
18
|
+
*/
|
|
19
|
+
export class DeterministicRandomGenerator {
|
|
20
|
+
/**
|
|
21
|
+
* @param in_seed - The initial seed (it can be either a GUID or a number)
|
|
22
|
+
* which is used to initialize the random number generator
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
constructor(in_seed) {
|
|
26
|
+
// Initialize the internal state from the given initial guid
|
|
27
|
+
if (_.isString(in_seed)) {
|
|
28
|
+
this._guid1 = GuidUtils.guidToUint32x4(in_seed);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
this._guid1 = GuidUtils.guidToUint32x4(calculateHash(String(in_seed)));
|
|
32
|
+
}
|
|
33
|
+
this._guid2 = new Uint32Array(4);
|
|
34
|
+
this._guid2[0] = (this._guid1[0] + 1) >>> 0;
|
|
35
|
+
this._guid2[1] = (this._guid1[1] + 1) >>> 0;
|
|
36
|
+
this._guid2[2] = (this._guid1[2] + 1) >>> 0;
|
|
37
|
+
this._guid2[3] = (this._guid1[3] + 1) >>> 0;
|
|
38
|
+
this._result = new Uint32Array(4);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a floating point random number
|
|
42
|
+
*
|
|
43
|
+
* @param in_max - If supplied the returned number will be 0 \<= number \< in_max. If none is given
|
|
44
|
+
* in_max = 1 is assumed
|
|
45
|
+
* @returns The random number
|
|
46
|
+
*/
|
|
47
|
+
random(in_max = 1.0) {
|
|
48
|
+
const randomInteger = this.irandom();
|
|
49
|
+
return randomInteger / 4294967296 * in_max;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates an integer point random number
|
|
53
|
+
*
|
|
54
|
+
* @param in_max - If supplied the returned number will be 0 \<= number \< in_max. If none is given
|
|
55
|
+
* in_max = 14294967296 (2^32) is assumed
|
|
56
|
+
* @returns The random number
|
|
57
|
+
*/
|
|
58
|
+
irandom(in_max) {
|
|
59
|
+
// Create a new hash
|
|
60
|
+
GuidUtils.hashCombine4xUint32(this._guid1, this._guid2, this._result);
|
|
61
|
+
// Permute the hashes
|
|
62
|
+
for (let i = 0; i < 4; i++) {
|
|
63
|
+
this._guid1[i] = this._guid2[i];
|
|
64
|
+
this._guid2[i] = this._result[i];
|
|
65
|
+
}
|
|
66
|
+
if (in_max === undefined) {
|
|
67
|
+
return this._guid1[0];
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
if (in_max < 16777619) {
|
|
71
|
+
// The random generator doesn't seem to be very good.
|
|
72
|
+
// It is quite biased (e.g. it generates too many even numbers)
|
|
73
|
+
// this is a hack to solve at least this problem, but we probably should
|
|
74
|
+
// instead use a different approach alltogether
|
|
75
|
+
return ((this._guid1[0]) % 16777619) % in_max;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return this._guid1[0] % in_max;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=deterministicRandomGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deterministicRandomGenerator.js","sourceRoot":"","sources":["../src/deterministicRandomGenerator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AAEH,+BAA+B;AAE/B,OAAO,CAAC,MAAM,QAAQ,CAAC;AACvB,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD;;;;;;GAMG;AACH,MAAM,OAAO,4BAA4B;IAKrC;;;;OAIG;IACH,YAAY,OAAwB;QAChC,4DAA4D;QAC5D,IAAI,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;YACrB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;SACnD;aAAM;YACH,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC,cAAc,CAAC,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;SAC1E;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;QACjC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QAC5C,IAAI,CAAC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AC/D,wEAAwE;gBACxE,+CAA+C;gBAC/C,OAAO,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,GAAG,MAAM,CAAC;aACjD;iBAAM;gBACH,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;aAClC;SACJ;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * @fileoverview Helper class to create a deterministic sequence of random numbers.\n */\n\n/* eslint-disable no-bitwise */\n\nimport _ from \"lodash\";\nimport { GuidUtils } from \"./guidUtils\";\nimport { calculateHash } from \"./hashCalculator\";\n\n/**\n * Random number generator that creates a deterministic sequence of random numbers based on an initial seed GUID\n *\n * Warning: This is a very straight forward implementation based on the hashCombine4xUint32 function. It probably\n * doesn't produce very high quality random numbers (do not use this for cryptography!) and it is not very\n * efficient.\n */\nexport class DeterministicRandomGenerator {\n _guid1: Uint32Array;\n _guid2: Uint32Array;\n _result: Uint32Array;\n\n /**\n * @param in_seed - The initial seed (it can be either a GUID or a number)\n * which is used to initialize the random number generator\n *\n */\n constructor(in_seed: string | number) {\n // Initialize the internal state from the given initial guid\n if (_.isString(in_seed)) {\n this._guid1 = GuidUtils.guidToUint32x4(in_seed);\n } else {\n this._guid1 = GuidUtils.guidToUint32x4(calculateHash(String(in_seed)));\n }\n this._guid2 = new Uint32Array(4);\n this._guid2[0] = (this._guid1[0] + 1) >>> 0;\n this._guid2[1] = (this._guid1[1] + 1) >>> 0;\n this._guid2[2] = (this._guid1[2] + 1) >>> 0;\n this._guid2[3] = (this._guid1[3] + 1) >>> 0;\n\n this._result = new Uint32Array(4);\n }\n\n /**\n * Creates a floating point random number\n *\n * @param in_max - If supplied the returned number will be 0 \\<= number \\< in_max. If none is given\n * in_max = 1 is assumed\n * @returns The random number\n */\n random(in_max = 1.0) {\n const randomInteger = this.irandom();\n return randomInteger / 4294967296 * in_max;\n }\n\n /**\n * Creates an integer point random number\n *\n * @param in_max - If supplied the returned number will be 0 \\<= number \\< in_max. If none is given\n * in_max = 14294967296 (2^32) is assumed\n * @returns The random number\n */\n irandom(in_max?: number): number {\n // Create a new hash\n GuidUtils.hashCombine4xUint32(this._guid1, this._guid2, this._result);\n\n // Permute the hashes\n for (let i = 0; i < 4; i++) {\n this._guid1[i] = this._guid2[i];\n this._guid2[i] = this._result[i];\n }\n\n if (in_max === undefined) {\n return this._guid1[0];\n } else {\n if (in_max < 16777619) {\n // The random generator doesn't seem to be very good.\n // It is quite biased (e.g. it generates too many even numbers)\n // this is a hack to solve at least this problem, but we probably should\n // instead use a different approach alltogether\n return ((this._guid1[0]) % 16777619) % in_max;\n } else {\n return this._guid1[0] % in_max;\n }\n }\n }\n}\n"]}
|