@mitm/common
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -0,1 +1,2 @@ | ||
export * from './stopwatch'; | ||
export * from './array'; | ||
@@ -7,2 +8,3 @@ export * from './debug'; | ||
export * from './javascript'; | ||
export * from './metadata'; | ||
export * from './nullable'; | ||
@@ -9,0 +11,0 @@ export * from './promise'; |
122
index.esm.js
import { shareReplay } from 'rxjs/operators'; | ||
class Stopwatch { | ||
constructor() { | ||
this.isRunning = false; | ||
this.timestamps = []; | ||
this.addedMilliseconds = []; | ||
} | ||
elapsed(allowNegative = false) { | ||
const startStopPairs = arrayPairwise(this.timestamps, new Date().valueOf()); | ||
const elapsed = startStopPairs.reduce((count, [start, stop]) => count + stop - start, 0); | ||
const advanced = this.addedMilliseconds.reduce((count, added) => count + added, elapsed); | ||
return allowNegative ? advanced : Math.max(0, advanced); | ||
} | ||
start() { | ||
if (this.isRunning) return; | ||
this.isRunning = true; | ||
this.timestamps.push(new Date().valueOf()); | ||
} | ||
stop() { | ||
if (!this.isRunning) return; | ||
this.isRunning = false; | ||
this.timestamps.push(new Date().valueOf()); | ||
} | ||
reset(stop = true) { | ||
this.timestamps = []; | ||
this.addedMilliseconds = []; | ||
if (!stop && this.isRunning) { | ||
// restart | ||
this.isRunning = false; | ||
this.start(); | ||
} | ||
} | ||
advanceBy(millis) { | ||
this.addedMilliseconds.push(millis); | ||
} | ||
} | ||
/** | ||
* Hosts multiple stopwatches, identified by a key. | ||
* Does not allow more than one stopwatch running at a given time, as long as the {@link start} method from this class | ||
* is being used to start a stopwatch. | ||
*/ | ||
class StopwatchMutexMap extends Map { | ||
for(key) { | ||
if (!this.has(key)) { | ||
this.set(key, new Stopwatch()); | ||
} | ||
return this.get(key); | ||
} | ||
start(key) { | ||
this.forEach(stopwatch => stopwatch.stop()); | ||
this.for(key).start(); | ||
} | ||
} | ||
/** | ||
* Evaluates a value that may be a thunk (arity=0), or returns the value directly. | ||
@@ -135,2 +200,57 @@ */ | ||
class MetadataError extends Error { | ||
constructor(documentId, problem) { | ||
const message = `Document "${documentId !== null && documentId !== void 0 ? documentId : 'unknown'}" has invalid metadata: ${problem}`; | ||
super(message); | ||
} | ||
} | ||
class MetadataKeyError extends MetadataError { | ||
constructor(documentId, key, problem) { | ||
const message = `@ key "${key}": ${problem}`; | ||
super(documentId, message); | ||
} | ||
} | ||
function hasKey(metadata, key) { | ||
return !!metadata && metadata.hasOwnProperty(key); | ||
} | ||
function getValue(documentId, metadata, key, typeGuard = val => typeof val == 'string') { | ||
if (!metadata) { | ||
return; | ||
} | ||
const value = metadata[key]; | ||
if (!value) { | ||
return; | ||
} | ||
if (!typeGuard(value)) { | ||
throw new MetadataKeyError(documentId, key, `the value "${value}" did not pass the type guard test, being ${typeGuard}`); | ||
} | ||
return value; | ||
} | ||
function getUnknownValue(documentId, metadata, key) { | ||
return getValue(documentId, metadata, key, val => true); | ||
} | ||
function requireValue(documentId, metadata, key, typeGuard = val => typeof val == 'string') { | ||
if (!metadata) { | ||
throw new MetadataError(documentId, 'metadata object is not present'); | ||
} | ||
if (!metadata.hasOwnProperty(key)) { | ||
throw new MetadataKeyError(documentId, key, 'key is missing'); | ||
} | ||
const value = metadata[key]; | ||
if (!typeGuard(value)) { | ||
throw new MetadataKeyError(documentId, key, `the value "${value}" did not pass the type guard test, being ${typeGuard}`); | ||
} | ||
return value; | ||
} | ||
function filterOutNullArrayEntries(array) { | ||
@@ -343,2 +463,2 @@ var _a; | ||
export { RolesHierarchy, all, any, arrayContainsId, arrayPairwise, ensureBoolean, ensureInEnum, ensureNotNull, ensureNumber, ensureString, equalIds, ethrow, evalThunk, filterOutNullArrayEntries, getStacktrace, normalizeToArray, shareReplayOne, swallowException, throwThisShouldNeverHappenError, throwUnexpectedTypeError, wait, waitThenReject }; | ||
export { MetadataError, MetadataKeyError, RolesHierarchy, Stopwatch, StopwatchMutexMap, all, any, arrayContainsId, arrayPairwise, ensureBoolean, ensureInEnum, ensureNotNull, ensureNumber, ensureString, equalIds, ethrow, evalThunk, filterOutNullArrayEntries, getStacktrace, getUnknownValue, getValue, hasKey, normalizeToArray, requireValue, shareReplayOne, swallowException, throwThisShouldNeverHappenError, throwUnexpectedTypeError, wait, waitThenReject }; |
203
index.umd.js
@@ -7,2 +7,62 @@ (function (global, factory) { | ||
var Stopwatch = | ||
/** @class */ | ||
function () { | ||
function Stopwatch() { | ||
this.isRunning = false; | ||
this.timestamps = []; | ||
this.addedMilliseconds = []; | ||
} | ||
Stopwatch.prototype.elapsed = function (allowNegative) { | ||
if (allowNegative === void 0) { | ||
allowNegative = false; | ||
} | ||
var startStopPairs = arrayPairwise(this.timestamps, new Date().valueOf()); | ||
var elapsed = startStopPairs.reduce(function (count, _a) { | ||
var start = _a[0], | ||
stop = _a[1]; | ||
return count + stop - start; | ||
}, 0); | ||
var advanced = this.addedMilliseconds.reduce(function (count, added) { | ||
return count + added; | ||
}, elapsed); | ||
return allowNegative ? advanced : Math.max(0, advanced); | ||
}; | ||
Stopwatch.prototype.start = function () { | ||
if (this.isRunning) return; | ||
this.isRunning = true; | ||
this.timestamps.push(new Date().valueOf()); | ||
}; | ||
Stopwatch.prototype.stop = function () { | ||
if (!this.isRunning) return; | ||
this.isRunning = false; | ||
this.timestamps.push(new Date().valueOf()); | ||
}; | ||
Stopwatch.prototype.reset = function (stop) { | ||
if (stop === void 0) { | ||
stop = true; | ||
} | ||
this.timestamps = []; | ||
this.addedMilliseconds = []; | ||
if (!stop && this.isRunning) { | ||
// restart | ||
this.isRunning = false; | ||
this.start(); | ||
} | ||
}; | ||
Stopwatch.prototype.advanceBy = function (millis) { | ||
this.addedMilliseconds.push(millis); | ||
}; | ||
return Stopwatch; | ||
}(); | ||
/*! ***************************************************************************** | ||
@@ -22,3 +82,19 @@ Copyright (c) Microsoft Corporation. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
var extendStatics = function(d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
function __extends(d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
} | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
@@ -63,2 +139,35 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
/** | ||
* Hosts multiple stopwatches, identified by a key. | ||
* Does not allow more than one stopwatch running at a given time, as long as the {@link start} method from this class | ||
* is being used to start a stopwatch. | ||
*/ | ||
var StopwatchMutexMap = | ||
/** @class */ | ||
function (_super) { | ||
__extends(StopwatchMutexMap, _super); | ||
function StopwatchMutexMap() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
StopwatchMutexMap.prototype.for = function (key) { | ||
if (!this.has(key)) { | ||
this.set(key, new Stopwatch()); | ||
} | ||
return this.get(key); | ||
}; | ||
StopwatchMutexMap.prototype.start = function (key) { | ||
this.forEach(function (stopwatch) { | ||
return stopwatch.stop(); | ||
}); | ||
this.for(key).start(); | ||
}; | ||
return StopwatchMutexMap; | ||
}(Map); | ||
/** | ||
* Evaluates a value that may be a thunk (arity=0), or returns the value directly. | ||
@@ -277,2 +386,88 @@ */ | ||
var MetadataError = | ||
/** @class */ | ||
function (_super) { | ||
__extends(MetadataError, _super); | ||
function MetadataError(documentId, problem) { | ||
var _this = this; | ||
var message = "Document \"".concat(documentId !== null && documentId !== void 0 ? documentId : 'unknown', "\" has invalid metadata: ").concat(problem); | ||
_this = _super.call(this, message) || this; | ||
return _this; | ||
} | ||
return MetadataError; | ||
}(Error); | ||
var MetadataKeyError = | ||
/** @class */ | ||
function (_super) { | ||
__extends(MetadataKeyError, _super); | ||
function MetadataKeyError(documentId, key, problem) { | ||
var _this = this; | ||
var message = "@ key \"".concat(key, "\": ").concat(problem); | ||
_this = _super.call(this, documentId, message) || this; | ||
return _this; | ||
} | ||
return MetadataKeyError; | ||
}(MetadataError); | ||
function hasKey(metadata, key) { | ||
return !!metadata && metadata.hasOwnProperty(key); | ||
} | ||
function getValue(documentId, metadata, key, typeGuard) { | ||
if (typeGuard === void 0) { | ||
typeGuard = function (val) { | ||
return typeof val == 'string'; | ||
}; | ||
} | ||
if (!metadata) { | ||
return; | ||
} | ||
var value = metadata[key]; | ||
if (!value) { | ||
return; | ||
} | ||
if (!typeGuard(value)) { | ||
throw new MetadataKeyError(documentId, key, "the value \"".concat(value, "\" did not pass the type guard test, being ").concat(typeGuard)); | ||
} | ||
return value; | ||
} | ||
function getUnknownValue(documentId, metadata, key) { | ||
return getValue(documentId, metadata, key, function (val) { | ||
return true; | ||
}); | ||
} | ||
function requireValue(documentId, metadata, key, typeGuard) { | ||
if (typeGuard === void 0) { | ||
typeGuard = function (val) { | ||
return typeof val == 'string'; | ||
}; | ||
} | ||
if (!metadata) { | ||
throw new MetadataError(documentId, 'metadata object is not present'); | ||
} | ||
if (!metadata.hasOwnProperty(key)) { | ||
throw new MetadataKeyError(documentId, key, 'key is missing'); | ||
} | ||
var value = metadata[key]; | ||
if (!typeGuard(value)) { | ||
throw new MetadataKeyError(documentId, key, "the value \"".concat(value, "\" did not pass the type guard test, being ").concat(typeGuard)); | ||
} | ||
return value; | ||
} | ||
function filterOutNullArrayEntries(array) { | ||
@@ -516,3 +711,7 @@ var _a; | ||
exports.MetadataError = MetadataError; | ||
exports.MetadataKeyError = MetadataKeyError; | ||
exports.RolesHierarchy = RolesHierarchy; | ||
exports.Stopwatch = Stopwatch; | ||
exports.StopwatchMutexMap = StopwatchMutexMap; | ||
exports.all = all; | ||
@@ -532,3 +731,7 @@ exports.any = any; | ||
exports.getStacktrace = getStacktrace; | ||
exports.getUnknownValue = getUnknownValue; | ||
exports.getValue = getValue; | ||
exports.hasKey = hasKey; | ||
exports.normalizeToArray = normalizeToArray; | ||
exports.requireValue = requireValue; | ||
exports.shareReplayOne = shareReplayOne; | ||
@@ -535,0 +738,0 @@ exports.swallowException = swallowException; |
{ | ||
"name": "@mitm/common", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"peerDependencies": { | ||
@@ -5,0 +5,0 @@ "rxjs": "^7.4.0" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
49966
21
1238
0