@softwear/latestcollectioncore 1.0.83 → 1.0.85
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/articleStatus.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/pivotTable.d.ts +1 -0
- package/dist/pivotTable.js +52 -0
- package/package.json +1 -1
- package/src/articleStatus.ts +1 -1
- package/src/index.ts +1 -0
- package/src/pivotTable.ts +48 -0
- package/test/pivotTable.js +62 -0
package/dist/articleStatus.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -10,5 +10,6 @@ export { default as findSkuByBarcode } from './findSkuByBarcode';
|
|
|
10
10
|
export { default as round2 } from './round2';
|
|
11
11
|
export { default as transaction } from './transaction';
|
|
12
12
|
export { default as articleStatus } from './articleStatus';
|
|
13
|
+
export { default as pivotTable } from './pivotTable';
|
|
13
14
|
export * from './types';
|
|
14
15
|
export * from './consts';
|
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.articleStatus = exports.transaction = exports.round2 = exports.findSkuByBarcode = exports.sizeToMap = exports.hasOnlyDigits = exports.hashBrand = exports.getPreferedPropertyMappings = exports.isean13 = exports.ean13 = exports.deepCopy = exports.buildPropertyMappingFn = void 0;
|
|
20
|
+
exports.pivotTable = exports.articleStatus = exports.transaction = exports.round2 = exports.findSkuByBarcode = exports.sizeToMap = exports.hasOnlyDigits = exports.hashBrand = exports.getPreferedPropertyMappings = exports.isean13 = exports.ean13 = exports.deepCopy = exports.buildPropertyMappingFn = void 0;
|
|
21
21
|
var buildPropertyMappingFn_1 = require("./buildPropertyMappingFn");
|
|
22
22
|
Object.defineProperty(exports, "buildPropertyMappingFn", { enumerable: true, get: function () { return __importDefault(buildPropertyMappingFn_1).default; } });
|
|
23
23
|
var deepCopy_1 = require("./deepCopy");
|
|
@@ -42,5 +42,7 @@ var transaction_1 = require("./transaction");
|
|
|
42
42
|
Object.defineProperty(exports, "transaction", { enumerable: true, get: function () { return __importDefault(transaction_1).default; } });
|
|
43
43
|
var articleStatus_1 = require("./articleStatus");
|
|
44
44
|
Object.defineProperty(exports, "articleStatus", { enumerable: true, get: function () { return __importDefault(articleStatus_1).default; } });
|
|
45
|
+
var pivotTable_1 = require("./pivotTable");
|
|
46
|
+
Object.defineProperty(exports, "pivotTable", { enumerable: true, get: function () { return __importDefault(pivotTable_1).default; } });
|
|
45
47
|
__exportStar(require("./types"), exports);
|
|
46
48
|
__exportStar(require("./consts"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function pivotTable(data: any[], key: string, property: string, value: string): any[];
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Performs a simple pivot transformation.
|
|
3
|
+
// Expecting imput data to have only one object per key/property
|
|
4
|
+
// No calculations are performed, just rearranging valeues from rows to columns
|
|
5
|
+
//
|
|
6
|
+
// Input Data:
|
|
7
|
+
// +--------+-----------+-------+
|
|
8
|
+
// | region | category | sales |
|
|
9
|
+
// +--------+-----------+-------+
|
|
10
|
+
// | North | Furniture | 1500 |
|
|
11
|
+
// | North | Technology| 2000 |
|
|
12
|
+
// | South | Furniture | 1200 |
|
|
13
|
+
// +--------+-----------+-------+
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
// Transformation:
|
|
16
|
+
// 1. Group by 'region' (key)
|
|
17
|
+
// 2. Pivot on 'category' (property)
|
|
18
|
+
// 3. Use 'sales' (value) for the output
|
|
19
|
+
// Output Data:
|
|
20
|
+
// +--------+-----------+-----------+
|
|
21
|
+
// | region | Furniture | Technology |
|
|
22
|
+
// +--------+-----------+-----------+
|
|
23
|
+
// | North | 1500 | 2000 |
|
|
24
|
+
// | South | 1200 | 0 |
|
|
25
|
+
// +--------+-----------+-----------+
|
|
26
|
+
function pivotTable(data, key, property, value) {
|
|
27
|
+
if (!Array.isArray(data))
|
|
28
|
+
throw Error(`Parameter 'data' needs to be an array`);
|
|
29
|
+
if (typeof key != 'string')
|
|
30
|
+
throw Error(`Parameter 'key' needs to be a string`);
|
|
31
|
+
if (typeof property != 'string')
|
|
32
|
+
throw Error(`Parameter 'property' needs to be a string`);
|
|
33
|
+
if (typeof value != 'string')
|
|
34
|
+
throw Error(`Parameter 'value' needs to be a string`);
|
|
35
|
+
const uniqueProperties = new Set();
|
|
36
|
+
const pivotResult = {};
|
|
37
|
+
data.forEach((item) => {
|
|
38
|
+
const keyValue = item[key];
|
|
39
|
+
const propertyValue = item[property];
|
|
40
|
+
uniqueProperties.add(propertyValue);
|
|
41
|
+
if (!pivotResult[keyValue])
|
|
42
|
+
pivotResult[keyValue] = {};
|
|
43
|
+
pivotResult[keyValue][propertyValue] = item[value];
|
|
44
|
+
});
|
|
45
|
+
return Object.keys(pivotResult).map((keyValue) => {
|
|
46
|
+
const entry = { [key]: keyValue };
|
|
47
|
+
for (const prop of [...uniqueProperties].sort())
|
|
48
|
+
entry[prop] = pivotResult[keyValue][prop] || 0;
|
|
49
|
+
return entry;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
exports.default = pivotTable;
|
package/package.json
CHANGED
package/src/articleStatus.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -10,5 +10,6 @@ export { default as findSkuByBarcode } from './findSkuByBarcode'
|
|
|
10
10
|
export { default as round2 } from './round2'
|
|
11
11
|
export { default as transaction } from './transaction'
|
|
12
12
|
export { default as articleStatus } from './articleStatus'
|
|
13
|
+
export { default as pivotTable } from './pivotTable'
|
|
13
14
|
export * from './types'
|
|
14
15
|
export * from './consts'
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Performs a simple pivot transformation.
|
|
2
|
+
// Expecting imput data to have only one object per key/property
|
|
3
|
+
// No calculations are performed, just rearranging valeues from rows to columns
|
|
4
|
+
//
|
|
5
|
+
// Input Data:
|
|
6
|
+
// +--------+-----------+-------+
|
|
7
|
+
// | region | category | sales |
|
|
8
|
+
// +--------+-----------+-------+
|
|
9
|
+
// | North | Furniture | 1500 |
|
|
10
|
+
// | North | Technology| 2000 |
|
|
11
|
+
// | South | Furniture | 1200 |
|
|
12
|
+
// +--------+-----------+-------+
|
|
13
|
+
|
|
14
|
+
// Transformation:
|
|
15
|
+
// 1. Group by 'region' (key)
|
|
16
|
+
// 2. Pivot on 'category' (property)
|
|
17
|
+
// 3. Use 'sales' (value) for the output
|
|
18
|
+
|
|
19
|
+
// Output Data:
|
|
20
|
+
// +--------+-----------+-----------+
|
|
21
|
+
// | region | Furniture | Technology |
|
|
22
|
+
// +--------+-----------+-----------+
|
|
23
|
+
// | North | 1500 | 2000 |
|
|
24
|
+
// | South | 1200 | 0 |
|
|
25
|
+
// +--------+-----------+-----------+
|
|
26
|
+
|
|
27
|
+
export default function pivotTable(data: any[], key: string, property: string, value: string): any[] {
|
|
28
|
+
if (!Array.isArray(data)) throw Error(`Parameter 'data' needs to be an array`)
|
|
29
|
+
if (typeof key != 'string') throw Error(`Parameter 'key' needs to be a string`)
|
|
30
|
+
if (typeof property != 'string') throw Error(`Parameter 'property' needs to be a string`)
|
|
31
|
+
if (typeof value != 'string') throw Error(`Parameter 'value' needs to be a string`)
|
|
32
|
+
const uniqueProperties = new Set()
|
|
33
|
+
const pivotResult = {}
|
|
34
|
+
|
|
35
|
+
data.forEach((item) => {
|
|
36
|
+
const keyValue = item[key]
|
|
37
|
+
const propertyValue = item[property]
|
|
38
|
+
uniqueProperties.add(propertyValue)
|
|
39
|
+
if (!pivotResult[keyValue]) pivotResult[keyValue] = {}
|
|
40
|
+
pivotResult[keyValue][propertyValue] = item[value]
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
return Object.keys(pivotResult).map((keyValue) => {
|
|
44
|
+
const entry = { [key]: keyValue }
|
|
45
|
+
for (const prop of [...uniqueProperties].sort()) entry[prop as string] = pivotResult[keyValue][prop] || 0
|
|
46
|
+
return entry
|
|
47
|
+
})
|
|
48
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const { pivotTable } = require('../dist/index')
|
|
2
|
+
const { expect } = require('chai')
|
|
3
|
+
|
|
4
|
+
const should = require('chai').should()
|
|
5
|
+
|
|
6
|
+
describe('pivotTable function', function () {
|
|
7
|
+
it('should thros on bad input', function () {
|
|
8
|
+
should.throw(() => {
|
|
9
|
+
pivotTable('123')
|
|
10
|
+
})
|
|
11
|
+
should.throw(() => {
|
|
12
|
+
pivotTable([], 3.14)
|
|
13
|
+
})
|
|
14
|
+
should.throw(() => {
|
|
15
|
+
pivotTable([], 'key', 3.14)
|
|
16
|
+
})
|
|
17
|
+
should.throw(() => {
|
|
18
|
+
pivotTable([], 'key', 'property', 3.14)
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
it('should transform rows to columns', function () {
|
|
22
|
+
const data = [
|
|
23
|
+
{ region: 'North', category: 'Furniture', sales: 1500 },
|
|
24
|
+
{ region: 'North', category: 'Technology', sales: 2000 },
|
|
25
|
+
{ region: 'South', category: 'Furniture', sales: 1200 },
|
|
26
|
+
]
|
|
27
|
+
should.throw(() => {
|
|
28
|
+
pivotTable(data, 3.14)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const expectedOutput = [
|
|
32
|
+
{ region: 'North', Furniture: 1500, Technology: 2000 },
|
|
33
|
+
{ region: 'South', Furniture: 1200, Technology: 0 },
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
const actualOutput = pivotTable(data, 'region', 'category', 'sales')
|
|
37
|
+
expect(actualOutput).to.deep.equal(expectedOutput)
|
|
38
|
+
})
|
|
39
|
+
it('should transform rows to columns', function () {
|
|
40
|
+
should.throw(() => {
|
|
41
|
+
pivotTable('123')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const data = [
|
|
45
|
+
{ group: 'Mexx\t123\twhite', size: '004:XL', qty: 1 },
|
|
46
|
+
{ group: 'Mexx\t123\twhite', size: '001:S', qty: 1 },
|
|
47
|
+
{ group: 'Mexx\t123\tblack', size: '001:S', qty: 1 },
|
|
48
|
+
{ group: 'Mexx\t123\tblack', size: '002:M', qty: 1 },
|
|
49
|
+
{ group: 'Mexx\t123\tblack', size: '003:L', qty: 1 },
|
|
50
|
+
{ group: 'Mexx\t123\tblack', size: '004:XL', qty: 1 },
|
|
51
|
+
{ group: 'Mexx\t443\tgreen', size: '004:XL', qty: 1 },
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
const expectedOutput = [
|
|
55
|
+
{ group: 'Mexx\t123\twhite', '001:S': 1, '002:M': 0, '003:L': 0, '004:XL': 1 },
|
|
56
|
+
{ group: 'Mexx\t123\tblack', '001:S': 1, '002:M': 1, '003:L': 1, '004:XL': 1 },
|
|
57
|
+
{ group: 'Mexx\t443\tgreen', '001:S': 0, '002:M': 0, '003:L': 0, '004:XL': 1 },
|
|
58
|
+
]
|
|
59
|
+
const actualOutput = pivotTable(data, 'group', 'size', 'qty')
|
|
60
|
+
expect(actualOutput).to.deep.equal(expectedOutput)
|
|
61
|
+
})
|
|
62
|
+
})
|