@configura/web-utilities 1.3.0-alpha.1 → 1.3.0-alpha.5
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/__tests__/convertLength.test.ts +37 -0
- package/dist/GenericCache.d.ts +8 -0
- package/dist/GenericCache.js +19 -1
- package/dist/Observable/LogObservable.js +3 -8
- package/dist/cmMips.d.ts +1 -0
- package/dist/cmMips.js +8 -6
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/utilitiesString.d.ts +2 -0
- package/dist/utilitiesString.js +12 -0
- package/dist/utilitiesUnits.d.ts +12 -0
- package/dist/utilitiesUnits.js +61 -0
- package/package.json +2 -2
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { convertLength, toLengthUnit } from "../dist/utilitiesUnits";
|
|
2
|
+
|
|
3
|
+
test("to m", () => {
|
|
4
|
+
expect(toLengthUnit("M")).toBe("m");
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
test("to in", () => {
|
|
8
|
+
expect(toLengthUnit("Inch")).toBe("in");
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("ft to ft", () => {
|
|
12
|
+
expect(convertLength(2, "ft", "ft")).toBe(2);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("cm to m", () => {
|
|
16
|
+
expect(convertLength(2, "cm", "m")).toBe(0.02);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("dm to mm", () => {
|
|
20
|
+
expect(convertLength(12.34, "dm", "mm")).toBe(1234);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("ft to mm", () => {
|
|
24
|
+
expect(convertLength(12.34, "ft", "mm")).toBeCloseTo(3761.232, 3);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("cm to inch", () => {
|
|
28
|
+
expect(convertLength(5, "cm", "in")).toBeCloseTo(1.96850394, 3);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("feet to inch", () => {
|
|
32
|
+
expect(convertLength(1, "ft", "in")).toBe(12);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("inch to feet", () => {
|
|
36
|
+
expect(convertLength(12, "in", "ft")).toBe(1);
|
|
37
|
+
});
|
package/dist/GenericCache.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
export declare class GenericCache<Key, Item> {
|
|
2
|
+
private _keyLookupFunction?;
|
|
3
|
+
/** @remarks Using a keyLoopupFunction makes the cache much much slower! */
|
|
4
|
+
constructor(_keyLookupFunction?: ((key1: Key, key2: Key) => boolean) | undefined);
|
|
2
5
|
_cache: Map<Key, Item>;
|
|
3
6
|
get(key: Key, createMissing: () => Item): Item;
|
|
4
7
|
delete: (key: Key) => boolean;
|
|
5
8
|
clear: () => void;
|
|
9
|
+
/**
|
|
10
|
+
* This might seem unnecessary, but for non-primitive datatypes equality is compared on
|
|
11
|
+
* reference, not value, so finding the right key is a thing that must be done.
|
|
12
|
+
*/
|
|
13
|
+
findExistingKeyOrReturnInput(key: Key, keyLookupFunction: (key1: Key, key2: Key) => boolean): Key;
|
|
6
14
|
}
|
|
7
15
|
//# sourceMappingURL=GenericCache.d.ts.map
|
package/dist/GenericCache.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export class GenericCache {
|
|
2
|
-
|
|
2
|
+
/** @remarks Using a keyLoopupFunction makes the cache much much slower! */
|
|
3
|
+
constructor(_keyLookupFunction) {
|
|
4
|
+
this._keyLookupFunction = _keyLookupFunction;
|
|
3
5
|
this._cache = new Map();
|
|
4
6
|
this.delete = (key) => {
|
|
5
7
|
return this._cache.delete(key);
|
|
@@ -9,6 +11,10 @@ export class GenericCache {
|
|
|
9
11
|
};
|
|
10
12
|
}
|
|
11
13
|
get(key, createMissing) {
|
|
14
|
+
key =
|
|
15
|
+
this._keyLookupFunction === undefined
|
|
16
|
+
? key
|
|
17
|
+
: this.findExistingKeyOrReturnInput(key, this._keyLookupFunction);
|
|
12
18
|
let item = this._cache.get(key);
|
|
13
19
|
if (item !== undefined) {
|
|
14
20
|
return item;
|
|
@@ -17,4 +23,16 @@ export class GenericCache {
|
|
|
17
23
|
this._cache.set(key, item);
|
|
18
24
|
return item;
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* This might seem unnecessary, but for non-primitive datatypes equality is compared on
|
|
28
|
+
* reference, not value, so finding the right key is a thing that must be done.
|
|
29
|
+
*/
|
|
30
|
+
findExistingKeyOrReturnInput(key, keyLookupFunction) {
|
|
31
|
+
for (const otherKey of this._cache.keys()) {
|
|
32
|
+
if (keyLookupFunction(key, otherKey)) {
|
|
33
|
+
return otherKey;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return key;
|
|
37
|
+
}
|
|
20
38
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { hashCode } from "../index.js";
|
|
1
2
|
import { AccumulatingObservable } from "./AccumulatingObservable.js";
|
|
2
3
|
export var LogLevel;
|
|
3
4
|
(function (LogLevel) {
|
|
@@ -16,13 +17,7 @@ export class LogMessage {
|
|
|
16
17
|
return `${this.level}_${this.message}_${this.optionalParams.join("_")}`;
|
|
17
18
|
}
|
|
18
19
|
get levelMessageHash() {
|
|
19
|
-
|
|
20
|
-
let hash = 0;
|
|
21
|
-
for (let i = 0; i < k.length; i++) {
|
|
22
|
-
const chr = k.charCodeAt(i);
|
|
23
|
-
hash = (hash << 5) - hash + chr;
|
|
24
|
-
}
|
|
25
|
-
return hash;
|
|
20
|
+
return hashCode(`${this.level}_${this.message}`);
|
|
26
21
|
}
|
|
27
22
|
equals(other) {
|
|
28
23
|
return this.key === other.key;
|
|
@@ -67,7 +62,7 @@ export class LogObservable extends AccumulatingObservable {
|
|
|
67
62
|
static set accumulate(v) {
|
|
68
63
|
this._accumulate = v;
|
|
69
64
|
if (!v) {
|
|
70
|
-
|
|
65
|
+
this.clear();
|
|
71
66
|
}
|
|
72
67
|
}
|
|
73
68
|
static get allMessages() {
|
package/dist/cmMips.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LogObservable } from "./Observable/LogObservable.js";
|
|
2
|
+
/** The mime type (or media type) is only used internally, so using the "x." sub type is OK. */
|
|
2
3
|
export declare const CMMIPS_MIMETYPE = "image/x.cmmips";
|
|
3
4
|
/**
|
|
4
5
|
* Extracts the specified MIP level image data from the CmMips container.
|
package/dist/cmMips.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
/** The mime type (or media type) is only used internally, so using the "x." sub type is OK. */
|
|
2
2
|
export const CMMIPS_MIMETYPE = "image/x.cmmips";
|
|
3
3
|
const CMMIPS_MAGIC = 0xee77;
|
|
4
|
-
|
|
4
|
+
/** Set to true to get verbose CmMips parsing info to the console. */
|
|
5
5
|
const CMMIPS_VERBOSE = false;
|
|
6
6
|
/**
|
|
7
7
|
* Extracts the specified MIP level image data from the CmMips container.
|
|
@@ -115,10 +115,12 @@ export class CmMip {
|
|
|
115
115
|
this.data = data;
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Helper class for reading the serialized CmMips header.
|
|
120
|
+
*
|
|
121
|
+
* The string format is from CM / DEX (?) and called "TinyNanoCStr".
|
|
122
|
+
* Numbers are stored in little endian.
|
|
123
|
+
*/
|
|
122
124
|
class CmMipsStream {
|
|
123
125
|
constructor(bytes) {
|
|
124
126
|
this.pos = 0;
|
package/dist/index.d.ts
CHANGED
|
@@ -15,5 +15,7 @@ export * from "./utilitiesImage.js";
|
|
|
15
15
|
export * from "./utilitiesMath.js";
|
|
16
16
|
export * from "./utilitiesPrice.js";
|
|
17
17
|
export * from "./utilitiesPromise.js";
|
|
18
|
+
export * from "./utilitiesString.js";
|
|
19
|
+
export * from "./utilitiesUnits.js";
|
|
18
20
|
export * from "./utilitiesWeb.js";
|
|
19
21
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -15,4 +15,6 @@ export * from "./utilitiesImage.js";
|
|
|
15
15
|
export * from "./utilitiesMath.js";
|
|
16
16
|
export * from "./utilitiesPrice.js";
|
|
17
17
|
export * from "./utilitiesPromise.js";
|
|
18
|
+
export * from "./utilitiesString.js";
|
|
19
|
+
export * from "./utilitiesUnits.js";
|
|
18
20
|
export * from "./utilitiesWeb.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function hashCode(str) {
|
|
2
|
+
const len = str.length;
|
|
3
|
+
let hash = 0, i, chr;
|
|
4
|
+
if (len === 0)
|
|
5
|
+
return hash;
|
|
6
|
+
for (i = 0; i < len; i++) {
|
|
7
|
+
chr = str.charCodeAt(i);
|
|
8
|
+
hash = (hash << 5) - hash + chr;
|
|
9
|
+
hash |= 0; // Convert to 32bit integer
|
|
10
|
+
}
|
|
11
|
+
return hash;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare type LengthUnit = "mm" | "cm" | "dm" | "m" | "in" | "ft";
|
|
2
|
+
/**
|
|
3
|
+
* Converts string to LengthUnit.
|
|
4
|
+
* @throws an error if the supplied string isn't a known length unit
|
|
5
|
+
*/
|
|
6
|
+
export declare function toLengthUnit(unit: string): LengthUnit;
|
|
7
|
+
/**
|
|
8
|
+
* As we accept inch as an alias for in we can't make this a typeguard
|
|
9
|
+
*/
|
|
10
|
+
export declare function isLengthUnit(unit: string): boolean;
|
|
11
|
+
export declare function convertLength(l: number, from: LengthUnit, to: LengthUnit): number;
|
|
12
|
+
//# sourceMappingURL=utilitiesUnits.d.ts.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const metricUnits = ["mm", "cm", "dm", "m"];
|
|
2
|
+
const meterPerInch = 0.0254;
|
|
3
|
+
const inchPerFeet = 12;
|
|
4
|
+
const meterPerFeet = meterPerInch * inchPerFeet;
|
|
5
|
+
/**
|
|
6
|
+
* Converts string to LengthUnit.
|
|
7
|
+
* @throws an error if the supplied string isn't a known length unit
|
|
8
|
+
*/
|
|
9
|
+
export function toLengthUnit(unit) {
|
|
10
|
+
unit = unit === null || unit === void 0 ? void 0 : unit.toLowerCase();
|
|
11
|
+
if (unit === "inch") {
|
|
12
|
+
unit = "in";
|
|
13
|
+
}
|
|
14
|
+
if (unit === "mm" ||
|
|
15
|
+
unit === "cm" ||
|
|
16
|
+
unit === "dm" ||
|
|
17
|
+
unit === "m" ||
|
|
18
|
+
unit === "in" ||
|
|
19
|
+
unit === "ft") {
|
|
20
|
+
return unit;
|
|
21
|
+
}
|
|
22
|
+
throw new Error(`Unknown length unit ${unit}`);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* As we accept inch as an alias for in we can't make this a typeguard
|
|
26
|
+
*/
|
|
27
|
+
export function isLengthUnit(unit) {
|
|
28
|
+
return (metricUnits.find((m) => m === unit) !== undefined ||
|
|
29
|
+
unit === "in" ||
|
|
30
|
+
unit === "inch" ||
|
|
31
|
+
unit === "ft");
|
|
32
|
+
}
|
|
33
|
+
export function convertLength(l, from, to) {
|
|
34
|
+
if (from === "ft" && to === "in") {
|
|
35
|
+
return l * inchPerFeet;
|
|
36
|
+
}
|
|
37
|
+
if (from === "in" && to === "ft") {
|
|
38
|
+
return l / inchPerFeet;
|
|
39
|
+
}
|
|
40
|
+
if (from === "ft") {
|
|
41
|
+
l *= meterPerFeet;
|
|
42
|
+
from = "m";
|
|
43
|
+
}
|
|
44
|
+
else if (from === "in") {
|
|
45
|
+
l *= meterPerInch;
|
|
46
|
+
from = "m";
|
|
47
|
+
}
|
|
48
|
+
if (to === "ft") {
|
|
49
|
+
l /= meterPerFeet;
|
|
50
|
+
to = "m";
|
|
51
|
+
}
|
|
52
|
+
else if (to === "in") {
|
|
53
|
+
l /= meterPerInch;
|
|
54
|
+
to = "m";
|
|
55
|
+
}
|
|
56
|
+
if (from === to) {
|
|
57
|
+
return l;
|
|
58
|
+
}
|
|
59
|
+
l *= Math.pow(10, metricUnits.indexOf(from) - metricUnits.indexOf(to));
|
|
60
|
+
return l;
|
|
61
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@configura/web-utilities",
|
|
3
|
-
"version": "1.3.0-alpha.
|
|
3
|
+
"version": "1.3.0-alpha.5",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
"publishConfig": {
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "7f22abef2c1047db092c871d9e9eceecbce5831e"
|
|
26
26
|
}
|