@softwear/latestcollectioncore 1.0.73 → 1.0.74
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/findSkuByBarcode.d.ts +2 -3
- package/dist/findSkuByBarcode.js +64 -26
- package/package.json +1 -1
- package/src/findSkuByBarcode.ts +62 -16
- package/test/findSkuByBarcode.spec.js +38 -12
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { MarkedSkuI } from './types';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
3
|
* Find a sku by 12 or 13 digit barcode.
|
|
5
4
|
* 12-digit US UPC barcodes can be shorthand for a 13-digit barcodes in the DB
|
|
6
5
|
* According to the UPC/EAN standard, US barcodes can omit the first 0 when printed
|
|
7
6
|
*/
|
|
8
|
-
export default function (
|
|
7
|
+
export default function (skus: {
|
|
9
8
|
[barcode: string]: MarkedSkuI;
|
|
10
|
-
}): MarkedSkuI | null;
|
|
9
|
+
}, barcodeToFind: string): MarkedSkuI | null;
|
package/dist/findSkuByBarcode.js
CHANGED
|
@@ -1,41 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
* Find a sku by barcode within an array.
|
|
5
|
+
*/
|
|
6
|
+
const lookupSkuInArray = function (skus, barcodeToFind) {
|
|
7
|
+
const foundSku = skus.find((sku) => sku.id === barcodeToFind);
|
|
8
|
+
return foundSku ? foundSku : null;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Find an alternative sku that have the same first 12 digits of the barcode within an array.
|
|
12
|
+
*/
|
|
13
|
+
const lookupAlternativeSkuInArray = function (skus, barcodeToFind) {
|
|
14
|
+
if (barcodeToFind.length !== 13)
|
|
15
|
+
return null;
|
|
16
|
+
const prefix = barcodeToFind.substring(0, 12);
|
|
17
|
+
const foundSku = skus.find((sku) => { var _a; return ((_a = sku.id) === null || _a === void 0 ? void 0 : _a.substring(0, 12)) === prefix; });
|
|
18
|
+
return foundSku ? foundSku : null;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Find a sku by barcode from an array.
|
|
22
|
+
*/
|
|
23
|
+
const findSkuByBarcodeFromArray = function (skus, barcodeToFind) {
|
|
24
|
+
let foundSku = lookupSkuInArray(skus, barcodeToFind);
|
|
25
|
+
if (foundSku)
|
|
26
|
+
return foundSku;
|
|
27
|
+
// This is an internally generated barcode.
|
|
28
|
+
// We have some weird errors from the past where incorrect barcodes were generated (wrong check digit.)
|
|
29
|
+
// When these barcodes were printed in our legacy software, noone noticed. Now, when LC prints these barcodes, the printer
|
|
30
|
+
// corrects the check digit. This results in a scan of a barcode that does not appear in the DB.
|
|
31
|
+
// However, we know which barcode we need because we can ignore the check digit.
|
|
32
|
+
foundSku = lookupAlternativeSkuInArray(skus, barcodeToFind);
|
|
33
|
+
if (foundSku)
|
|
34
|
+
return foundSku;
|
|
35
|
+
if (barcodeToFind.length != 12)
|
|
36
|
+
return null;
|
|
37
|
+
const barcode13 = '0' + barcodeToFind;
|
|
38
|
+
foundSku = lookupSkuInArray(skus, barcode13);
|
|
39
|
+
if (foundSku)
|
|
40
|
+
return foundSku;
|
|
41
|
+
return null;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Find a sku by barcode from an object.
|
|
8
45
|
*/
|
|
9
|
-
function
|
|
46
|
+
const findSkuByBarcodeFromObject = function (skus, barcodeToFind) {
|
|
10
47
|
if (skus[barcodeToFind])
|
|
11
48
|
return skus[barcodeToFind];
|
|
12
|
-
if (barcodeToFind.length == 13
|
|
49
|
+
if (barcodeToFind.length == 13) {
|
|
13
50
|
// This is an internally generated barcode.
|
|
14
51
|
// We have some weird errors from the past where incorrect barcodes were generated (wrong check digit.)
|
|
15
52
|
// When these barcodes were printed in our legacy software, noone noticed. Now, when LC prints these barcodes, the printer
|
|
16
53
|
// corrects the check digit. This results in a scan of a barcode that does not appear in the DB.
|
|
17
54
|
// However, we know which barcode we need because we can ignore the check digit.
|
|
18
55
|
const prefix = barcodeToFind.substring(0, 12);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return skus[prefix + '1'];
|
|
23
|
-
if (skus[prefix + '2'])
|
|
24
|
-
return skus[prefix + '2'];
|
|
25
|
-
if (skus[prefix + '3'])
|
|
26
|
-
return skus[prefix + '3'];
|
|
27
|
-
if (skus[prefix + '4'])
|
|
28
|
-
return skus[prefix + '4'];
|
|
29
|
-
if (skus[prefix + '5'])
|
|
30
|
-
return skus[prefix + '5'];
|
|
31
|
-
if (skus[prefix + '6'])
|
|
32
|
-
return skus[prefix + '6'];
|
|
33
|
-
if (skus[prefix + '7'])
|
|
34
|
-
return skus[prefix + '7'];
|
|
35
|
-
if (skus[prefix + '8'])
|
|
36
|
-
return skus[prefix + '8'];
|
|
37
|
-
if (skus[prefix + '9'])
|
|
38
|
-
return skus[prefix + '9'];
|
|
56
|
+
for (let i = 0; i <= 9; i++)
|
|
57
|
+
if (skus[prefix + `${i}`])
|
|
58
|
+
return skus[prefix + `${i}`];
|
|
39
59
|
}
|
|
40
60
|
if (barcodeToFind.length != 12)
|
|
41
61
|
return null;
|
|
@@ -43,5 +63,23 @@ function default_1(barcodeToFind, skus) {
|
|
|
43
63
|
if (skus[barcode13])
|
|
44
64
|
return skus[barcode13];
|
|
45
65
|
return null;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Check whether the passed parameter is an object
|
|
69
|
+
*/
|
|
70
|
+
function isObject(variable) {
|
|
71
|
+
return variable !== null && variable !== undefined && typeof variable === 'object' && !Array.isArray(variable);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Find a sku by 12 or 13 digit barcode.
|
|
75
|
+
* 12-digit US UPC barcodes can be shorthand for a 13-digit barcodes in the DB
|
|
76
|
+
* According to the UPC/EAN standard, US barcodes can omit the first 0 when printed
|
|
77
|
+
*/
|
|
78
|
+
function default_1(skus, barcodeToFind) {
|
|
79
|
+
if (Array.isArray(skus))
|
|
80
|
+
return findSkuByBarcodeFromArray(skus, barcodeToFind);
|
|
81
|
+
if (isObject(skus))
|
|
82
|
+
return findSkuByBarcodeFromObject(skus, barcodeToFind);
|
|
83
|
+
throw Error(`findSkuByBarcode doesn't support type of ${typeof skus}, must be an object or an array.`);
|
|
46
84
|
}
|
|
47
85
|
exports.default = default_1;
|
package/package.json
CHANGED
package/src/findSkuByBarcode.ts
CHANGED
|
@@ -1,30 +1,58 @@
|
|
|
1
1
|
import { MarkedSkuI } from './types'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
* Find a sku by barcode within an array.
|
|
5
|
+
*/
|
|
6
|
+
const lookupSkuInArray = function (skus: MarkedSkuI[], barcodeToFind: string): MarkedSkuI | null {
|
|
7
|
+
const foundSku = skus.find((sku: MarkedSkuI) => sku.id === barcodeToFind)
|
|
8
|
+
return foundSku ? foundSku : null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Find an alternative sku that have the same first 12 digits of the barcode within an array.
|
|
13
|
+
*/
|
|
14
|
+
const lookupAlternativeSkuInArray = function (skus: MarkedSkuI[], barcodeToFind: string): MarkedSkuI | null {
|
|
15
|
+
if (barcodeToFind.length !== 13) return null
|
|
16
|
+
const prefix = barcodeToFind.substring(0, 12)
|
|
17
|
+
const foundSku = skus.find((sku: MarkedSkuI) => sku.id?.substring(0, 12) === prefix)
|
|
18
|
+
return foundSku ? foundSku : null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Find a sku by barcode from an array.
|
|
8
23
|
*/
|
|
9
|
-
|
|
24
|
+
const findSkuByBarcodeFromArray = function (skus: MarkedSkuI[], barcodeToFind: string): MarkedSkuI | null {
|
|
25
|
+
let foundSku = lookupSkuInArray(skus, barcodeToFind)
|
|
26
|
+
if (foundSku) return foundSku
|
|
27
|
+
|
|
28
|
+
// This is an internally generated barcode.
|
|
29
|
+
// We have some weird errors from the past where incorrect barcodes were generated (wrong check digit.)
|
|
30
|
+
// When these barcodes were printed in our legacy software, noone noticed. Now, when LC prints these barcodes, the printer
|
|
31
|
+
// corrects the check digit. This results in a scan of a barcode that does not appear in the DB.
|
|
32
|
+
// However, we know which barcode we need because we can ignore the check digit.
|
|
33
|
+
foundSku = lookupAlternativeSkuInArray(skus, barcodeToFind)
|
|
34
|
+
if (foundSku) return foundSku
|
|
35
|
+
|
|
36
|
+
if (barcodeToFind.length != 12) return null
|
|
37
|
+
const barcode13 = '0' + barcodeToFind
|
|
38
|
+
foundSku = lookupSkuInArray(skus, barcode13)
|
|
39
|
+
if (foundSku) return foundSku
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Find a sku by barcode from an object.
|
|
45
|
+
*/
|
|
46
|
+
const findSkuByBarcodeFromObject = function (skus: { [barcode: string]: MarkedSkuI }, barcodeToFind: string): MarkedSkuI | null {
|
|
10
47
|
if (skus[barcodeToFind]) return skus[barcodeToFind]
|
|
11
|
-
if (barcodeToFind.length == 13
|
|
48
|
+
if (barcodeToFind.length == 13) {
|
|
12
49
|
// This is an internally generated barcode.
|
|
13
50
|
// We have some weird errors from the past where incorrect barcodes were generated (wrong check digit.)
|
|
14
51
|
// When these barcodes were printed in our legacy software, noone noticed. Now, when LC prints these barcodes, the printer
|
|
15
52
|
// corrects the check digit. This results in a scan of a barcode that does not appear in the DB.
|
|
16
53
|
// However, we know which barcode we need because we can ignore the check digit.
|
|
17
54
|
const prefix = barcodeToFind.substring(0, 12)
|
|
18
|
-
if (skus[prefix +
|
|
19
|
-
if (skus[prefix + '1']) return skus[prefix + '1']
|
|
20
|
-
if (skus[prefix + '2']) return skus[prefix + '2']
|
|
21
|
-
if (skus[prefix + '3']) return skus[prefix + '3']
|
|
22
|
-
if (skus[prefix + '4']) return skus[prefix + '4']
|
|
23
|
-
if (skus[prefix + '5']) return skus[prefix + '5']
|
|
24
|
-
if (skus[prefix + '6']) return skus[prefix + '6']
|
|
25
|
-
if (skus[prefix + '7']) return skus[prefix + '7']
|
|
26
|
-
if (skus[prefix + '8']) return skus[prefix + '8']
|
|
27
|
-
if (skus[prefix + '9']) return skus[prefix + '9']
|
|
55
|
+
for (let i = 0; i <= 9; i++) if (skus[prefix + `${i}`]) return skus[prefix + `${i}`]
|
|
28
56
|
}
|
|
29
57
|
|
|
30
58
|
if (barcodeToFind.length != 12) return null
|
|
@@ -32,3 +60,21 @@ export default function (barcodeToFind: string, skus: { [barcode: string]: Marke
|
|
|
32
60
|
if (skus[barcode13]) return skus[barcode13]
|
|
33
61
|
return null
|
|
34
62
|
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Check whether the passed parameter is an object
|
|
66
|
+
*/
|
|
67
|
+
function isObject(variable: any): boolean {
|
|
68
|
+
return variable !== null && variable !== undefined && typeof variable === 'object' && !Array.isArray(variable);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Find a sku by 12 or 13 digit barcode.
|
|
73
|
+
* 12-digit US UPC barcodes can be shorthand for a 13-digit barcodes in the DB
|
|
74
|
+
* According to the UPC/EAN standard, US barcodes can omit the first 0 when printed
|
|
75
|
+
*/
|
|
76
|
+
export default function (skus: { [barcode: string]: MarkedSkuI }, barcodeToFind: string): MarkedSkuI | null {
|
|
77
|
+
if (Array.isArray(skus)) return findSkuByBarcodeFromArray(skus, barcodeToFind)
|
|
78
|
+
if (isObject(skus)) return findSkuByBarcodeFromObject(skus, barcodeToFind)
|
|
79
|
+
throw Error(`findSkuByBarcode doesn't support type of ${typeof skus}, must be an object or an array.`)
|
|
80
|
+
}
|
|
@@ -2,22 +2,48 @@ const { findSkuByBarcode } = require('../dist/index')
|
|
|
2
2
|
const { expect } = require('chai')
|
|
3
3
|
|
|
4
4
|
describe('findSkuByBarcode function', function () {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
// Test findSkuByBarcode function by passing skus parameter as an Object
|
|
6
|
+
it('(Skus Object) should return null for any barcode shorter than 12 digits', function () {
|
|
7
|
+
expect(findSkuByBarcode({}, '12345678901')).to.equal(null)
|
|
7
8
|
})
|
|
8
|
-
it('should return the sku at the first check if exist', function () {
|
|
9
|
-
expect(findSkuByBarcode('1234567890123'
|
|
9
|
+
it('(Skus Object) should return the sku at the first check if exist', function () {
|
|
10
|
+
expect(findSkuByBarcode({ '1234567890123': { id: '1234567890123' } }, '1234567890123')).to.deep.equal({ id: '1234567890123' })
|
|
10
11
|
})
|
|
11
|
-
it('should return the sku at the second check if exist, 13 digits
|
|
12
|
-
expect(findSkuByBarcode('
|
|
12
|
+
it('(Skus Object) should return the sku at the second check if exist with another last number, 13 digits', function () {
|
|
13
|
+
expect(findSkuByBarcode({ '1234567890127': { id: '1234567890127' } }, '1234567890123')).to.deep.equal({ id: '1234567890127' })
|
|
13
14
|
})
|
|
14
|
-
it('should return null because the sku doesn not exist', function () {
|
|
15
|
-
expect(findSkuByBarcode('2234567890123'
|
|
15
|
+
it('(Skus Object) should return null because the sku doesn not exist', function () {
|
|
16
|
+
expect(findSkuByBarcode({}, '2234567890123')).to.equal(null)
|
|
16
17
|
})
|
|
17
|
-
it('should return the sku at the third check if exist, 12 digits', function () {
|
|
18
|
-
expect(findSkuByBarcode(
|
|
18
|
+
it('(Skus Object) should return the sku at the third check if exist, 12 digits', function () {
|
|
19
|
+
expect(findSkuByBarcode({ '0123456789012': { id: '0123456789012' } }, '123456789012')).to.deep.equal({ id: '0123456789012' })
|
|
19
20
|
})
|
|
20
|
-
it('should return the null because the sku does not exist, 12 digits', function () {
|
|
21
|
-
expect(findSkuByBarcode('123456789012'
|
|
21
|
+
it('(Skus Object) should return the null because the sku does not exist, 12 digits', function () {
|
|
22
|
+
expect(findSkuByBarcode({}, '123456789012')).to.deep.equal(null)
|
|
23
|
+
})
|
|
24
|
+
// Test findSkuByBarcode function by passing skus parameter as an Array
|
|
25
|
+
it('(Skus Array) should return null for any barcode shorter than 12 digits', function () {
|
|
26
|
+
expect(findSkuByBarcode([], '12345678901')).to.equal(null)
|
|
27
|
+
})
|
|
28
|
+
it('(Skus Array) should return the sku at the first check if exist', function () {
|
|
29
|
+
expect(findSkuByBarcode([{ id: '1234567890123' }], '1234567890123')).to.deep.equal({ id: '1234567890123' })
|
|
30
|
+
})
|
|
31
|
+
it('(Skus Array) should return the sku at the second check if exist with another last number, 13 digits', function () {
|
|
32
|
+
expect(findSkuByBarcode([{ id: '1234567890127' }], '1234567890123')).to.deep.equal({ id: '1234567890127' })
|
|
33
|
+
})
|
|
34
|
+
it('(Skus Array) should return null because the sku doesn not exist', function () {
|
|
35
|
+
expect(findSkuByBarcode([], '2234567890123')).to.equal(null)
|
|
36
|
+
})
|
|
37
|
+
it('(Skus Array) should return the sku at the third check if exist, 12 digits', function () {
|
|
38
|
+
expect(findSkuByBarcode([{ id: '0123456789012' }], '123456789012')).to.deep.equal({ id: '0123456789012' })
|
|
39
|
+
})
|
|
40
|
+
it('(Skus Array) should return the null because the sku does not exist, 12 digits', function () {
|
|
41
|
+
expect(findSkuByBarcode([], '123456789012')).to.deep.equal(null)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should fail validation for unsupported skus type', function () {
|
|
45
|
+
expect(() => findSkuByBarcode(123, '123')).to.throw("findSkuByBarcode doesn't support type of number, must be an object or an array.")
|
|
46
|
+
expect(() => findSkuByBarcode('123', '123')).to.throw("findSkuByBarcode doesn't support type of string, must be an object or an array.")
|
|
47
|
+
expect(() => findSkuByBarcode(true, '123')).to.throw("findSkuByBarcode doesn't support type of boolean, must be an object or an array.")
|
|
22
48
|
})
|
|
23
49
|
})
|