@open-mercato/shared 0.4.5-develop-3ce83a8b24 → 0.4.5-develop-539cff4960
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.
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const REFERENCE_UNIT_CODES = ["kg", "l", "m2", "m3", "pc"];
|
|
2
|
+
const LEGACY_UNIT_CODE_ALIASES = {
|
|
3
|
+
qty: "pc"
|
|
4
|
+
};
|
|
5
|
+
function canonicalizeUnitCode(value) {
|
|
6
|
+
if (typeof value !== "string") return null;
|
|
7
|
+
const trimmed = value.trim();
|
|
8
|
+
if (!trimmed.length) return null;
|
|
9
|
+
const alias = LEGACY_UNIT_CODE_ALIASES[trimmed.toLowerCase()];
|
|
10
|
+
return alias ?? trimmed.toLowerCase();
|
|
11
|
+
}
|
|
12
|
+
function toUnitLookupKey(value) {
|
|
13
|
+
return canonicalizeUnitCode(value);
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
REFERENCE_UNIT_CODES,
|
|
17
|
+
canonicalizeUnitCode,
|
|
18
|
+
toUnitLookupKey
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=unitCodes.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/lib/units/unitCodes.ts"],
|
|
4
|
+
"sourcesContent": ["export const REFERENCE_UNIT_CODES = ['kg', 'l', 'm2', 'm3', 'pc'] as const;\nexport type ReferenceUnitCode = (typeof REFERENCE_UNIT_CODES)[number];\n\nconst LEGACY_UNIT_CODE_ALIASES: Record<string, string> = {\n qty: \"pc\",\n};\n\nexport function canonicalizeUnitCode(value: unknown): string | null {\n if (typeof value !== \"string\") return null;\n const trimmed = value.trim();\n if (!trimmed.length) return null;\n const alias = LEGACY_UNIT_CODE_ALIASES[trimmed.toLowerCase()];\n return alias ?? trimmed.toLowerCase();\n}\n\nexport function toUnitLookupKey(value: unknown): string | null {\n return canonicalizeUnitCode(value);\n}\n"],
|
|
5
|
+
"mappings": "AAAO,MAAM,uBAAuB,CAAC,MAAM,KAAK,MAAM,MAAM,IAAI;AAGhE,MAAM,2BAAmD;AAAA,EACvD,KAAK;AACP;AAEO,SAAS,qBAAqB,OAA+B;AAClE,MAAI,OAAO,UAAU,SAAU,QAAO;AACtC,QAAM,UAAU,MAAM,KAAK;AAC3B,MAAI,CAAC,QAAQ,OAAQ,QAAO;AAC5B,QAAM,QAAQ,yBAAyB,QAAQ,YAAY,CAAC;AAC5D,SAAO,SAAS,QAAQ,YAAY;AACtC;AAEO,SAAS,gBAAgB,OAA+B;AAC7D,SAAO,qBAAqB,KAAK;AACnC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/lib/version.js
CHANGED
package/dist/lib/version.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/version.ts"],
|
|
4
|
-
"sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.4.5-develop-
|
|
4
|
+
"sourcesContent": ["// Build-time generated version\nexport const APP_VERSION = '0.4.5-develop-539cff4960'\nexport const appVersion = APP_VERSION\n"],
|
|
5
5
|
"mappings": "AACO,MAAM,cAAc;AACpB,MAAM,aAAa;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { canonicalizeUnitCode, toUnitLookupKey } from '../unitCodes'
|
|
2
|
+
|
|
3
|
+
describe('canonicalizeUnitCode', () => {
|
|
4
|
+
it('returns "pc" for legacy alias "qty"', () => {
|
|
5
|
+
expect(canonicalizeUnitCode('qty')).toBe('pc')
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
it('returns "pc" for uppercase alias "QTY" (case insensitive)', () => {
|
|
9
|
+
expect(canonicalizeUnitCode('QTY')).toBe('pc')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('passes through codes without aliases unchanged', () => {
|
|
13
|
+
expect(canonicalizeUnitCode('m2')).toBe('m2')
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('lowercases non-alias unit codes', () => {
|
|
17
|
+
expect(canonicalizeUnitCode('KG')).toBe('kg')
|
|
18
|
+
expect(canonicalizeUnitCode('M2')).toBe('m2')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('returns null for empty string', () => {
|
|
22
|
+
expect(canonicalizeUnitCode('')).toBeNull()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('returns null for null', () => {
|
|
26
|
+
expect(canonicalizeUnitCode(null)).toBeNull()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('returns null for non-string values', () => {
|
|
30
|
+
expect(canonicalizeUnitCode(42)).toBeNull()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('trims whitespace before resolving', () => {
|
|
34
|
+
expect(canonicalizeUnitCode(' kg ')).toBe('kg')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('returns null for undefined', () => {
|
|
38
|
+
expect(canonicalizeUnitCode(undefined)).toBeNull()
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('returns null for whitespace-only string', () => {
|
|
42
|
+
expect(canonicalizeUnitCode(' ')).toBeNull()
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('toUnitLookupKey', () => {
|
|
47
|
+
it('lowercases a standard unit code', () => {
|
|
48
|
+
expect(toUnitLookupKey('KG')).toBe('kg')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('resolves alias and lowercases the result', () => {
|
|
52
|
+
expect(toUnitLookupKey('qty')).toBe('pc')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('returns null for null input', () => {
|
|
56
|
+
expect(toUnitLookupKey(null)).toBeNull()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('returns null for empty string', () => {
|
|
60
|
+
expect(toUnitLookupKey('')).toBeNull()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('returns null for non-string values', () => {
|
|
64
|
+
expect(toUnitLookupKey(123)).toBeNull()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('trims and lowercases mixed-case input', () => {
|
|
68
|
+
expect(toUnitLookupKey(' M2 ')).toBe('m2')
|
|
69
|
+
})
|
|
70
|
+
})
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const REFERENCE_UNIT_CODES = ['kg', 'l', 'm2', 'm3', 'pc'] as const;
|
|
2
|
+
export type ReferenceUnitCode = (typeof REFERENCE_UNIT_CODES)[number];
|
|
3
|
+
|
|
4
|
+
const LEGACY_UNIT_CODE_ALIASES: Record<string, string> = {
|
|
5
|
+
qty: "pc",
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export function canonicalizeUnitCode(value: unknown): string | null {
|
|
9
|
+
if (typeof value !== "string") return null;
|
|
10
|
+
const trimmed = value.trim();
|
|
11
|
+
if (!trimmed.length) return null;
|
|
12
|
+
const alias = LEGACY_UNIT_CODE_ALIASES[trimmed.toLowerCase()];
|
|
13
|
+
return alias ?? trimmed.toLowerCase();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function toUnitLookupKey(value: unknown): string | null {
|
|
17
|
+
return canonicalizeUnitCode(value);
|
|
18
|
+
}
|