@softwear/latestcollectioncore 1.0.172 → 1.0.174

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.
@@ -28,16 +28,23 @@ const evaluateFromExpr = function (sku, from) {
28
28
  return sku[from];
29
29
  return from.map((field) => sku[field] || '').join('.');
30
30
  };
31
+ const indexBrandMappings = function (index, key, propertyMapping) {
32
+ if (!key)
33
+ return;
34
+ if (!index[key])
35
+ index[key] = {};
36
+ propertyMapping === null || propertyMapping === void 0 ? void 0 : propertyMapping.forEach((mapping) => {
37
+ if (!index[key][mapping.category])
38
+ index[key][mapping.category] = {};
39
+ index[key][mapping.category][mapping.from] = mapping.to;
40
+ });
41
+ };
31
42
  function default_1(brands, strategy) {
32
43
  const indexedByBrandPropertyMapping = {};
33
- brands.forEach(({ id, propertyMapping }) => {
34
- if (!indexedByBrandPropertyMapping[id])
35
- indexedByBrandPropertyMapping[id] = {};
36
- propertyMapping === null || propertyMapping === void 0 ? void 0 : propertyMapping.forEach((mapping) => {
37
- if (!indexedByBrandPropertyMapping[id][mapping.category])
38
- indexedByBrandPropertyMapping[id][mapping.category] = {};
39
- indexedByBrandPropertyMapping[id][mapping.category][mapping.from] = mapping.to;
40
- });
44
+ brands.forEach(({ id, collection, aliases, propertyMapping }) => {
45
+ indexBrandMappings(indexedByBrandPropertyMapping, id, propertyMapping);
46
+ // Alias-aware indexing prevents missing mappings when SKU brandhash resolves to an alias hash.
47
+ aliases === null || aliases === void 0 ? void 0 : aliases.forEach((alias) => indexBrandMappings(indexedByBrandPropertyMapping, alias, propertyMapping));
41
48
  });
42
49
  if (strategy == 'clean')
43
50
  return function (sku) {
@@ -0,0 +1,2 @@
1
+ import { BrandSettingI } from './types';
2
+ export default function (brand: string, brandsByHash: Record<string, BrandSettingI>, brands: BrandSettingI[], hashedBrand?: string): BrandSettingI | undefined;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const hashBrand_1 = __importDefault(require("./hashBrand"));
7
+ function isBrandEqual(hashedBrand, collection, aliases) {
8
+ return hashedBrand == collection || (aliases === null || aliases === void 0 ? void 0 : aliases.includes(hashedBrand));
9
+ }
10
+ function default_1(brand, brandsByHash, brands, hashedBrand = (0, hashBrand_1.default)(brand)) {
11
+ const mappedBrand = brandsByHash[hashedBrand];
12
+ if (mappedBrand)
13
+ return mappedBrand;
14
+ return brands.find((candidate) => isBrandEqual(hashedBrand, candidate.collection, candidate.aliases));
15
+ }
16
+ exports.default = default_1;
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export { default as edifact } from './edifact';
6
6
  export { default as ensureArray } from './ensureArray';
7
7
  export { default as ensureImpliedProperties } from './ensureImpliedProperties';
8
8
  export { default as findSkuByBarcode } from './findSkuByBarcode';
9
+ export { default as findMetaBrandSetting } from './findMetaBrandSetting';
9
10
  export { default as getBrandName } from './getBrandName';
10
11
  export { default as getPreferedPropertyMappings } from './getPreferedPropertyMappings';
11
12
  export { default as hashBrand } from './hashBrand';
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.isAxiosError = exports.exponentialDelay = exports.axiosRetry = exports.createAxiosInstance = exports.lcAxios = exports.transaction = exports.sizeToMap = exports.round2 = exports.pivotTable = exports.isean13 = exports.imageBinder = exports.hasOnlyDigits = exports.hashBrand = exports.getPreferedPropertyMappings = exports.getBrandName = exports.findSkuByBarcode = exports.ensureImpliedProperties = exports.ensureArray = exports.edifact = exports.ean13 = exports.deepCopy = exports.buildPropertyMappingFn = exports.articleStatus = void 0;
20
+ exports.isAxiosError = exports.exponentialDelay = exports.axiosRetry = exports.createAxiosInstance = exports.lcAxios = exports.transaction = exports.sizeToMap = exports.round2 = exports.pivotTable = exports.isean13 = exports.imageBinder = exports.hasOnlyDigits = exports.hashBrand = exports.getPreferedPropertyMappings = exports.getBrandName = exports.findMetaBrandSetting = exports.findSkuByBarcode = exports.ensureImpliedProperties = exports.ensureArray = exports.edifact = exports.ean13 = exports.deepCopy = exports.buildPropertyMappingFn = exports.articleStatus = void 0;
21
21
  var articleStatus_1 = require("./articleStatus");
22
22
  Object.defineProperty(exports, "articleStatus", { enumerable: true, get: function () { return __importDefault(articleStatus_1).default; } });
23
23
  var buildPropertyMappingFn_1 = require("./buildPropertyMappingFn");
@@ -34,6 +34,8 @@ var ensureImpliedProperties_1 = require("./ensureImpliedProperties");
34
34
  Object.defineProperty(exports, "ensureImpliedProperties", { enumerable: true, get: function () { return __importDefault(ensureImpliedProperties_1).default; } });
35
35
  var findSkuByBarcode_1 = require("./findSkuByBarcode");
36
36
  Object.defineProperty(exports, "findSkuByBarcode", { enumerable: true, get: function () { return __importDefault(findSkuByBarcode_1).default; } });
37
+ var findMetaBrandSetting_1 = require("./findMetaBrandSetting");
38
+ Object.defineProperty(exports, "findMetaBrandSetting", { enumerable: true, get: function () { return __importDefault(findMetaBrandSetting_1).default; } });
37
39
  var getBrandName_1 = require("./getBrandName");
38
40
  Object.defineProperty(exports, "getBrandName", { enumerable: true, get: function () { return __importDefault(getBrandName_1).default; } });
39
41
  var getPreferedPropertyMappings_1 = require("./getPreferedPropertyMappings");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softwear/latestcollectioncore",
3
- "version": "1.0.172",
3
+ "version": "1.0.174",
4
4
  "description": "Core functions for LatestCollections applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -36,14 +36,21 @@ interface propertyMappingI {
36
36
  }
37
37
  }
38
38
 
39
+ const indexBrandMappings = function (index: propertyMappingI, key: string | undefined, propertyMapping: BrandSettingI['propertyMapping']) {
40
+ if (!key) return
41
+ if (!index[key]) index[key] = {}
42
+ propertyMapping?.forEach((mapping) => {
43
+ if (!index[key][mapping.category]) index[key][mapping.category] = {}
44
+ index[key][mapping.category][mapping.from] = mapping.to
45
+ })
46
+ }
47
+
39
48
  export default function (brands: Array<BrandSettingI>, strategy: mappingStrategyE): (sku: SkuI) => SkuI {
40
49
  const indexedByBrandPropertyMapping = {} as propertyMappingI
41
- brands.forEach(({ id, propertyMapping }) => {
42
- if (!indexedByBrandPropertyMapping[id]) indexedByBrandPropertyMapping[id] = {}
43
- propertyMapping?.forEach((mapping) => {
44
- if (!indexedByBrandPropertyMapping[id][mapping.category]) indexedByBrandPropertyMapping[id][mapping.category] = {}
45
- indexedByBrandPropertyMapping[id][mapping.category][mapping.from] = mapping.to
46
- })
50
+ brands.forEach(({ id, collection, aliases, propertyMapping }) => {
51
+ indexBrandMappings(indexedByBrandPropertyMapping, id, propertyMapping)
52
+ // Alias-aware indexing prevents missing mappings when SKU brandhash resolves to an alias hash.
53
+ aliases?.forEach((alias) => indexBrandMappings(indexedByBrandPropertyMapping, alias, propertyMapping))
47
54
  })
48
55
  if (strategy == 'clean')
49
56
  return function (sku: SkuI): SkuI {
@@ -0,0 +1,18 @@
1
+ import hashBrand from './hashBrand'
2
+ import { BrandSettingI } from './types'
3
+
4
+ function isBrandEqual(hashedBrand: string, collection?: string, aliases?: string[]) {
5
+ return hashedBrand == collection || aliases?.includes(hashedBrand)
6
+ }
7
+
8
+ export default function (
9
+ brand: string,
10
+ brandsByHash: Record<string, BrandSettingI>,
11
+ brands: BrandSettingI[],
12
+ hashedBrand = hashBrand(brand)
13
+ ): BrandSettingI | undefined {
14
+ const mappedBrand = brandsByHash[hashedBrand]
15
+ if (mappedBrand) return mappedBrand
16
+
17
+ return brands.find((candidate) => isBrandEqual(hashedBrand, candidate.collection, candidate.aliases))
18
+ }
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ export { default as edifact } from './edifact'
6
6
  export { default as ensureArray } from './ensureArray'
7
7
  export { default as ensureImpliedProperties } from './ensureImpliedProperties'
8
8
  export { default as findSkuByBarcode } from './findSkuByBarcode'
9
+ export { default as findMetaBrandSetting } from './findMetaBrandSetting'
9
10
  export { default as getBrandName } from './getBrandName'
10
11
  export { default as getPreferedPropertyMappings } from './getPreferedPropertyMappings'
11
12
  export { default as hashBrand } from './hashBrand'
@@ -451,4 +451,26 @@ describe('applyPropertyMapping', () => {
451
451
  const mappedSkus = applyPropertyMapping(deepCopy(skus), dtbMapping)
452
452
  expect(mappedSkus[11].DTBGROUP).toBe('77777')
453
453
  })
454
+
455
+ it('applies mapping when sku.brandhash matches an alias', function () {
456
+ const aliasSkus = [
457
+ {
458
+ id: 'alias-1',
459
+ brand: 'SummumWomen',
460
+ brandhash: 'summumwomen',
461
+ colorCodeSupplier: '100',
462
+ colorSupplier: 'Black',
463
+ },
464
+ ]
465
+ const aliasMapping = [
466
+ {
467
+ id: 'summum',
468
+ collection: 'summum',
469
+ aliases: ['summumwomen'],
470
+ propertyMapping: [{ category: 'ColorCode', from: '100', to: 'AliasColor' }],
471
+ },
472
+ ]
473
+ const mappedSkus = applyPropertyMapping(deepCopy(aliasSkus), aliasMapping)
474
+ expect(mappedSkus[0].colorFamily).toBe('AliasColor')
475
+ })
454
476
  })
@@ -0,0 +1,21 @@
1
+ const { findMetaBrandSetting, hashBrand } = require('../dist/index')
2
+
3
+ describe('findMetaBrandSetting function', function () {
4
+ it('returns direct hash match from brandsByHash', function () {
5
+ const brand = { id: 'summum', name: 'Summum', collection: 'summum' }
6
+ const result = findMetaBrandSetting('Summum', { summum: brand }, [brand])
7
+ expect(result).toBe(brand)
8
+ })
9
+
10
+ it('returns alias fallback match when direct hash is missing', function () {
11
+ const brand = { id: 'summum', name: 'Summum', collection: 'summum', aliases: [hashBrand('summumwomen')] }
12
+ const result = findMetaBrandSetting('SummumWomen', {}, [brand])
13
+ expect(result).toBe(brand)
14
+ })
15
+
16
+ it('returns undefined when no match is found', function () {
17
+ const brand = { id: 'summum', name: 'Summum', collection: 'summum', aliases: [hashBrand('summumwomen')] }
18
+ const result = findMetaBrandSetting('DifferentBrand', {}, [brand])
19
+ expect(result).toBeUndefined()
20
+ })
21
+ })