store-scrapper-js-common 1.0.164 → 1.0.166
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/entities/product.d.ts +1 -0
- package/dist/entities/product.js +4 -0
- package/dist/entities/product.js.map +1 -1
- package/dist/utils/array-utils.d.ts +1 -0
- package/dist/utils/array-utils.js +7 -1
- package/dist/utils/array-utils.js.map +1 -1
- package/dist/utils/index.d.ts +3 -3
- package/dist/utils/index.js +5 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/number-utils.d.ts +2 -0
- package/dist/utils/number-utils.js +19 -1
- package/dist/utils/number-utils.js.map +1 -1
- package/package.json +1 -1
- package/src/entities/product.ts +4 -1
- package/src/utils/array-utils.ts +8 -0
- package/src/utils/index.ts +5 -3
- package/src/utils/number-utils.ts +25 -0
- package/tests/utils/array-utils.test.ts +19 -1
- package/tests/utils/number-utils.test.ts +51 -1
package/dist/entities/product.js
CHANGED
|
@@ -270,6 +270,10 @@ __decorate([
|
|
|
270
270
|
typeorm_1.Column(),
|
|
271
271
|
__metadata("design:type", Date)
|
|
272
272
|
], Product.prototype, "extractedAt", void 0);
|
|
273
|
+
__decorate([
|
|
274
|
+
typeorm_1.Column(),
|
|
275
|
+
__metadata("design:type", Boolean)
|
|
276
|
+
], Product.prototype, "isFree", void 0);
|
|
273
277
|
Product = __decorate([
|
|
274
278
|
typeorm_1.Entity({ name: 'products' }),
|
|
275
279
|
typeorm_1.Unique('store_ref_sku', ['storeRef', 'sku'])
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"product.js","sourceRoot":"/","sources":["entities/product.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAGiB;AACjB,qDAA6C;AAC7C,mDAA+C;AAC/C,mCAAgC;AAChC,iDAA6C;AAE7C,0DAAsD;AAGtD,IAAY,YAWX;AAXD,WAAY,YAAY;IACpB,2BAAW,CAAA;IACX,mCAAmB,CAAA;IACnB,uCAAu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AAM,EAAE;;mDACqB;AAG9B;IADC,gBAAM,EAAE;;4CACa;AAGtB;IADC,gBAAM,EAAE;;mDACmB;AAG5B;IADC,gBAAM,EAAE;;oDACoB;AAG7B;IADC,gBAAM,EAAE;;yCACW;AAGpB;IADC,gBAAM,EAAE;8BACK,IAAI;4CAAC;AAGnB;IADC,gBAAM,EAAE;;uCACQ;AA1KR,OAAO;IAFnB,gBAAM,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;IAC5B,gBAAM,CAAC,eAAe,EAAE,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;GAChC,OAAO,CA2KnB;AA3KY,0BAAO","sourcesContent":["import {\n Column,\n Entity, Index, Unique,\n} from 'typeorm';\nimport { IsNotEmpty } from 'class-validator';\nimport { AbstractBase } from './abstract-base';\nimport { Price } from './price';\nimport { PricesStats } from './prices-stats';\nimport { Keyword } from './keyword';\nimport { HttpRequest } from '../classes/http-request';\nimport { ImageTypeEnum } from '../enums/image-type.enum';\n\nexport enum CurrencyEnum {\n CLP = 'clp',\n CMR_PTS = 'cmr_pts',\n PARIS_PTS = 'paris_pts',\n RIPLEY_PTS = 'ripley_pts',\n GENERAL_POINTS = 'general_pts',\n BRL = 'brl',\n PEN = 'pen',\n ARS = 'ars',\n EUR = 'eur',\n USD = 'usd',\n}\n\nexport enum UpdateReasonEnum {\n TIME = 'time',\n NEW_PRICE = 'new_price',\n PRICE_DOWN = 'price_down',\n PRICE_UP = 'price_up',\n NEW_CONDITIONAL_PRICE = 'new_conditional_price',\n NEW_NAME = 'new_name',\n NEW_LISTING = 'new_listing',\n NEW_STOCK = 'new_stock',\n STOCK_UP = 'stock_up',\n STOCK_DOWN = 'stock_down',\n MISSING_DATA = 'missing_data',\n FIX_DATA = 'fix_data',\n RE_LISTING = 're_listing',\n OUT_OF_STOCK = 'out_of_stock',\n\n CHILDREN_NEW_PRICE = 'children_new_price',\n CHILDREN_NEW_CONDITIONAL_PRICE = 'children_conditional_new_price',\n CHILDREN_NEW_NAME = 'children_new_name',\n CHILDREN_NEW_LISTING = 'children_new_listing',\n CHILDREN_NEW_STOCK = 'children_new_stock',\n CHILDREN_MISSING_DATA = 'children_missing_data',\n CHILDREN_FIX_DATA = 'children_fix_data',\n CHILDREN_RE_LISTING = 'children_re_listing',\n CHILDREN_OUT_OF_STOCK = 'children_out_of_stock'\n}\n\nexport interface ProductAttributes {\n stock?: number;\n\n // TODO: Add enum\n color?: string;\n\n // TODO: Add enum\n size?: string;\n\n [key: string]: any;\n}\n\nexport interface ConditionalPrice {\n minAmount?: number;\n price?:number;\n cardRequired?: boolean;\n createdAt?: Date;\n updatedAt?: Date;\n}\n\n@Entity({ name: 'products' })\n@Unique('store_ref_sku', ['storeRef', 'sku'])\nexport class Product extends AbstractBase {\n @Column()\n name: string;\n\n @Column()\n brandName?: string;\n\n @Column()\n normalizedBrandName?: string;\n\n @Column()\n modelName?: string;\n\n @Column()\n @IsNotEmpty()\n @Index()\n sku: string;\n\n @Column()\n internalId?: string;\n\n @Column()\n ean?: string;\n\n @Column()\n @IsNotEmpty()\n url: string;\n\n @Column()\n @IsNotEmpty()\n @Index()\n storeRef: string;\n\n @Column()\n storeName?: string;\n\n @Column()\n branchStoreName?: string;\n\n @Column()\n sellerName?: string;\n\n @Column()\n normalizedSellerName?: string;\n\n @Column()\n @IsNotEmpty()\n price?: Price;\n\n @Column()\n conditionalPrice?: ConditionalPrice;\n\n @Column()\n conditionalPriceHistory?: ConditionalPrice[];\n\n @Column()\n previousPrice?: Price;\n\n @Column()\n bestPrice?: Price;\n\n @Column()\n priceStats?: PricesStats;\n\n @Column()\n priceHistory?: Price[];\n\n @Column()\n rank?: number;\n\n @Column()\n image?: string;\n\n @Column()\n images?: { type: ImageTypeEnum, url: string }[]\n\n @Column()\n category?: string;\n\n @Column()\n categoryPath?: string;\n\n @Column()\n page?: number;\n\n @Column()\n userRank?: number;\n\n @Column()\n attributes?: ProductAttributes;\n\n @Column()\n stockHistory?: {\n stock: number,\n updatedAt: string,\n }[];\n\n @Column()\n homeDeliveryShipping?: boolean;\n\n @Column()\n pickUpFromStoreShipping?: boolean;\n\n @Column()\n fastDelivery?: boolean;\n\n @Column()\n enabled?: boolean;\n\n @Column()\n enabledHistory?: {enabled: boolean, updatedAt?:Date, createdAt?: Date}[];\n\n @Column()\n sourceUrl?: string;\n\n @Column()\n request? : HttpRequest;\n\n @Column()\n @Index()\n updatedAt?: Date;\n\n @Column()\n @Index()\n categoryKeywordNames?: string[];\n\n @Column()\n categoryKeywords?: Keyword[];\n\n @Column()\n @Index()\n processedPricesAt?: Date;\n\n @Column()\n isMarketplace?: boolean;\n\n @Column()\n addToCartLink?: string;\n\n @Column()\n currency?: CurrencyEnum;\n\n @Column()\n updateReason?: UpdateReasonEnum[];\n\n @Column()\n nameHistory?: {\n name: string,\n updatedAt: string,\n }[];\n\n @Column()\n availableLocations?: string[];\n\n @Column()\n refurbished?: boolean;\n\n @Column()\n parentProductIdRef?: string;\n\n @Column()\n parentProductSkuRef?: string;\n\n @Column()\n children?: Product[]\n\n @Column()\n extractedAt?: Date;\n\n @Column()\n isFree?: boolean;\n}\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export declare const isArrayEmpty: <T>(arr: T[]) => boolean;
|
|
2
2
|
export declare const hasElementInArray: <T>(elements: T[], searchElements: T[]) => boolean;
|
|
3
3
|
export declare const stringToArray: (str: string, splitBy?: string) => string[];
|
|
4
|
+
export declare const getRandomItem: <T>(items: T[]) => T;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stringToArray = exports.hasElementInArray = exports.isArrayEmpty = void 0;
|
|
3
|
+
exports.getRandomItem = exports.stringToArray = exports.hasElementInArray = exports.isArrayEmpty = void 0;
|
|
4
4
|
exports.isArrayEmpty = (arr) => {
|
|
5
5
|
if (arr && !Array.isArray(arr)) {
|
|
6
6
|
throw Error('Element is not an array');
|
|
@@ -19,4 +19,10 @@ exports.stringToArray = (str, splitBy = ',') => {
|
|
|
19
19
|
}
|
|
20
20
|
return str.split(splitBy).filter((e) => e);
|
|
21
21
|
};
|
|
22
|
+
exports.getRandomItem = (items) => {
|
|
23
|
+
if (exports.isArrayEmpty(items)) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return items[Math.floor(Math.random() * items.length)];
|
|
27
|
+
};
|
|
22
28
|
//# sourceMappingURL=array-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array-utils.js","sourceRoot":"/","sources":["utils/array-utils.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG,CAAI,GAAQ,EAAW,EAAE;IACnD,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC9B,MAAM,KAAK,CAAC,yBAAyB,CAAC,CAAC;KACxC;IAED,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,CAAC;AAClC,CAAC,CAAC;AAEW,QAAA,iBAAiB,GAAG,CAAI,QAAa,EAAE,cAAmB,EAAW,EAAE;IAClF,IAAI,oBAAY,CAAC,QAAQ,CAAC,IAAI,oBAAY,CAAC,cAAc,CAAC,EAAE;QAC1D,OAAO,KAAK,CAAC;KACd;IAED,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,kBAAkB,EAAE,EAAE,CAAC,YAAY,KAAK,kBAAkB,CAAC,CAAC,CAAC;AAC3H,CAAC,CAAC;AAEW,QAAA,aAAa,GAAG,CAAC,GAAW,EAAE,OAAO,GAAG,GAAG,EAAY,EAAE;IACpE,IAAI,CAAC,GAAG,EAAE;QACR,OAAO,IAAI,CAAC;KACb;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;AAC7C,CAAC,CAAC","sourcesContent":["export const isArrayEmpty = <T>(arr: T[]): boolean => {\n if (arr && !Array.isArray(arr)) {\n throw Error('Element is not an array');\n }\n\n return !arr || arr.length === 0;\n};\n\nexport const hasElementInArray = <T>(elements: T[], searchElements: T[]): boolean => {\n if (isArrayEmpty(elements) || isArrayEmpty(searchElements)) {\n return false;\n }\n\n return elements.some((updateReason) => searchElements.some((searchUpdateReason) => updateReason === searchUpdateReason));\n};\n\nexport const stringToArray = (str: string, splitBy = ','): string[] => {\n if (!str) {\n return null;\n }\n\n return str.split(splitBy).filter((e) => e);\n};\n"]}
|
|
1
|
+
{"version":3,"file":"array-utils.js","sourceRoot":"/","sources":["utils/array-utils.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG,CAAI,GAAQ,EAAW,EAAE;IACnD,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC9B,MAAM,KAAK,CAAC,yBAAyB,CAAC,CAAC;KACxC;IAED,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,CAAC;AAClC,CAAC,CAAC;AAEW,QAAA,iBAAiB,GAAG,CAAI,QAAa,EAAE,cAAmB,EAAW,EAAE;IAClF,IAAI,oBAAY,CAAC,QAAQ,CAAC,IAAI,oBAAY,CAAC,cAAc,CAAC,EAAE;QAC1D,OAAO,KAAK,CAAC;KACd;IAED,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,kBAAkB,EAAE,EAAE,CAAC,YAAY,KAAK,kBAAkB,CAAC,CAAC,CAAC;AAC3H,CAAC,CAAC;AAEW,QAAA,aAAa,GAAG,CAAC,GAAW,EAAE,OAAO,GAAG,GAAG,EAAY,EAAE;IACpE,IAAI,CAAC,GAAG,EAAE;QACR,OAAO,IAAI,CAAC;KACb;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;AAC7C,CAAC,CAAC;AAEW,QAAA,aAAa,GAAG,CAAI,KAAU,EAAK,EAAE;IAChD,IAAI,oBAAY,CAAC,KAAK,CAAC,EAAE;QACvB,OAAO,IAAI,CAAC;KACb;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AACzD,CAAC,CAAC","sourcesContent":["export const isArrayEmpty = <T>(arr: T[]): boolean => {\n if (arr && !Array.isArray(arr)) {\n throw Error('Element is not an array');\n }\n\n return !arr || arr.length === 0;\n};\n\nexport const hasElementInArray = <T>(elements: T[], searchElements: T[]): boolean => {\n if (isArrayEmpty(elements) || isArrayEmpty(searchElements)) {\n return false;\n }\n\n return elements.some((updateReason) => searchElements.some((searchUpdateReason) => updateReason === searchUpdateReason));\n};\n\nexport const stringToArray = (str: string, splitBy = ','): string[] => {\n if (!str) {\n return null;\n }\n\n return str.split(splitBy).filter((e) => e);\n};\n\nexport const getRandomItem = <T>(items: T[]): T => {\n if (isArrayEmpty(items)) {\n return null;\n }\n\n return items[Math.floor(Math.random() * items.length)];\n};\n"]}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { checkIfSamePrices, isCheaper, getMinPrice, isValidPrice, getAvgPrice, g
|
|
|
2
2
|
import { findLastQuery } from './entity-queries-utils';
|
|
3
3
|
import { reverseString, removeNewLines, includesStringArray, getLastAfter, replaceMultipleSpacesByOne, removeIfEndsWith } from './string-utils';
|
|
4
4
|
import { isUrl, getHostname, getParam, getPathname } from './url-utils';
|
|
5
|
-
import { calculateAverage, roundNumber } from './number-utils';
|
|
6
|
-
import { hasElementInArray, isArrayEmpty, stringToArray } from './array-utils';
|
|
5
|
+
import { calculateAverage, roundNumber, getRandomNumber, getRandomNumbers, getPercentageDiff } from './number-utils';
|
|
6
|
+
import { hasElementInArray, isArrayEmpty, stringToArray, getRandomItem } from './array-utils';
|
|
7
7
|
import { hasUpdateReason, getStoreName } from './product.utils';
|
|
8
8
|
import { formatDate, formatMoney, formatPct } from './string-formatter';
|
|
9
|
-
export { checkIfSamePrices, findLastQuery, replaceMultipleSpacesByOne, hasMeasurementUnitMultiplier, getPriceByMeasurementUnitMultiplier, removeIfEndsWith, getLastAfter, includesStringArray, removeNewLines, reverseString, getParam, getPathname, getHostname, isUrl, isValidPrice, getMinPrice, isCheaper, getMaxPrice, getPriceOperation, getAvgPrice, calculateAverage, roundNumber, removeZeroPrices, isArrayEmpty, hasElementInArray, stringToArray, hasUpdateReason, getStoreName, formatPct, formatDate, formatMoney, };
|
|
9
|
+
export { checkIfSamePrices, findLastQuery, replaceMultipleSpacesByOne, hasMeasurementUnitMultiplier, getPriceByMeasurementUnitMultiplier, removeIfEndsWith, getLastAfter, includesStringArray, removeNewLines, reverseString, getParam, getPathname, getHostname, isUrl, isValidPrice, getMinPrice, isCheaper, getMaxPrice, getPriceOperation, getAvgPrice, calculateAverage, roundNumber, removeZeroPrices, isArrayEmpty, hasElementInArray, stringToArray, hasUpdateReason, getStoreName, formatPct, formatDate, formatMoney, getRandomNumber, getRandomNumbers, getPercentageDiff, getRandomItem, };
|
package/dist/utils/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatMoney = exports.formatDate = exports.formatPct = exports.getStoreName = exports.hasUpdateReason = exports.stringToArray = exports.hasElementInArray = exports.isArrayEmpty = exports.removeZeroPrices = exports.roundNumber = exports.calculateAverage = exports.getAvgPrice = exports.getPriceOperation = exports.getMaxPrice = exports.isCheaper = exports.getMinPrice = exports.isValidPrice = exports.isUrl = exports.getHostname = exports.getPathname = exports.getParam = exports.reverseString = exports.removeNewLines = exports.includesStringArray = exports.getLastAfter = exports.removeIfEndsWith = exports.getPriceByMeasurementUnitMultiplier = exports.hasMeasurementUnitMultiplier = exports.replaceMultipleSpacesByOne = exports.findLastQuery = exports.checkIfSamePrices = void 0;
|
|
3
|
+
exports.getRandomItem = exports.getPercentageDiff = exports.getRandomNumbers = exports.getRandomNumber = exports.formatMoney = exports.formatDate = exports.formatPct = exports.getStoreName = exports.hasUpdateReason = exports.stringToArray = exports.hasElementInArray = exports.isArrayEmpty = exports.removeZeroPrices = exports.roundNumber = exports.calculateAverage = exports.getAvgPrice = exports.getPriceOperation = exports.getMaxPrice = exports.isCheaper = exports.getMinPrice = exports.isValidPrice = exports.isUrl = exports.getHostname = exports.getPathname = exports.getParam = exports.reverseString = exports.removeNewLines = exports.includesStringArray = exports.getLastAfter = exports.removeIfEndsWith = exports.getPriceByMeasurementUnitMultiplier = exports.hasMeasurementUnitMultiplier = exports.replaceMultipleSpacesByOne = exports.findLastQuery = exports.checkIfSamePrices = void 0;
|
|
4
4
|
const price_utils_1 = require("./price-utils");
|
|
5
5
|
Object.defineProperty(exports, "checkIfSamePrices", { enumerable: true, get: function () { return price_utils_1.checkIfSamePrices; } });
|
|
6
6
|
Object.defineProperty(exports, "isCheaper", { enumerable: true, get: function () { return price_utils_1.isCheaper; } });
|
|
@@ -29,10 +29,14 @@ Object.defineProperty(exports, "getPathname", { enumerable: true, get: function
|
|
|
29
29
|
const number_utils_1 = require("./number-utils");
|
|
30
30
|
Object.defineProperty(exports, "calculateAverage", { enumerable: true, get: function () { return number_utils_1.calculateAverage; } });
|
|
31
31
|
Object.defineProperty(exports, "roundNumber", { enumerable: true, get: function () { return number_utils_1.roundNumber; } });
|
|
32
|
+
Object.defineProperty(exports, "getRandomNumber", { enumerable: true, get: function () { return number_utils_1.getRandomNumber; } });
|
|
33
|
+
Object.defineProperty(exports, "getRandomNumbers", { enumerable: true, get: function () { return number_utils_1.getRandomNumbers; } });
|
|
34
|
+
Object.defineProperty(exports, "getPercentageDiff", { enumerable: true, get: function () { return number_utils_1.getPercentageDiff; } });
|
|
32
35
|
const array_utils_1 = require("./array-utils");
|
|
33
36
|
Object.defineProperty(exports, "hasElementInArray", { enumerable: true, get: function () { return array_utils_1.hasElementInArray; } });
|
|
34
37
|
Object.defineProperty(exports, "isArrayEmpty", { enumerable: true, get: function () { return array_utils_1.isArrayEmpty; } });
|
|
35
38
|
Object.defineProperty(exports, "stringToArray", { enumerable: true, get: function () { return array_utils_1.stringToArray; } });
|
|
39
|
+
Object.defineProperty(exports, "getRandomItem", { enumerable: true, get: function () { return array_utils_1.getRandomItem; } });
|
|
36
40
|
const product_utils_1 = require("./product.utils");
|
|
37
41
|
Object.defineProperty(exports, "hasUpdateReason", { enumerable: true, get: function () { return product_utils_1.hasUpdateReason; } });
|
|
38
42
|
Object.defineProperty(exports, "getStoreName", { enumerable: true, get: function () { return product_utils_1.getStoreName; } });
|
package/dist/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["utils/index.ts"],"names":[],"mappings":";;;AAAA,+CAGuB;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["utils/index.ts"],"names":[],"mappings":";;;AAAA,+CAGuB;AAmBrB,kGArBA,+BAAiB,OAqBA;AAE8B,0FAvB5B,uBAAS,OAuB4B;AAAtB,4FAvBJ,yBAAW,OAuBI;AAAzB,6FAvBuB,0BAAY,OAuBvB;AAA0D,4FAvBjC,yBAAW,OAuBiC;AAA3C,4FAvBY,yBAAW,OAuBZ;AAAE,kGAvBY,+BAAiB,OAuBZ;AAC3E,iGAxByF,8BAAgB,OAwBzF;AAHiC,6GApB9D,0CAA4B,OAoB8D;AAAE,oHApB9D,iDAAmC,OAoB8D;AAlBjI,iEAAuD;AAkBlC,8FAlBZ,oCAAa,OAkBY;AAjBlC,iDAGwB;AAe+C,8FAjBrE,4BAAa,OAiBqE;AAA7B,+FAjBtC,6BAAc,OAiBsC;AAAnC,oGAjBD,kCAAmB,OAiBC;AAAjC,6FAjBkC,2BAAY,OAiBlC;AADI,2GAhBgC,yCAA0B,OAgBhC;AAC5D,iGAjB8F,+BAAgB,OAiB9F;AAdlB,2CAEqB;AAaN,sFAdb,iBAAK,OAca;AAAlB,4FAdO,uBAAW,OAcP;AADyE,yFAbhE,oBAAQ,OAagE;AAAE,4FAbhE,uBAAW,OAagE;AAX3G,iDAEwB;AAUiF,iGAXvG,+BAAgB,OAWuG;AACvH,4FAZkB,0BAAW,OAYlB;AACyB,gGAbL,8BAAe,OAaK;AAAE,iGAbL,+BAAgB,OAaK;AAAE,kGAbL,gCAAiB,OAaK;AAX1F,+CAEuB;AAQwB,kGAT7C,+BAAiB,OAS6C;AAA/B,6FATZ,0BAAY,OASY;AAAqB,8FAT/B,2BAAa,OAS+B;AACa,8FAV1C,2BAAa,OAU0C;AARzG,mDAAgE;AAOiB,gGAPxE,+BAAe,OAOwE;AAAE,6FAPxE,4BAAY,OAOwE;AAN9G,yDAAwE;AAO3D,2FAPJ,6BAAU,OAOI;AAAE,4FAPJ,8BAAW,OAOI;AAAlC,0FAPgC,4BAAS,OAOhC","sourcesContent":["import {\n checkIfSamePrices, isCheaper, getMinPrice, isValidPrice, getAvgPrice, getMaxPrice, getPriceOperation, removeZeroPrices,\n hasMeasurementUnitMultiplier, getPriceByMeasurementUnitMultiplier,\n} from './price-utils';\nimport { findLastQuery } from './entity-queries-utils';\nimport {\n reverseString, removeNewLines, includesStringArray, getLastAfter, replaceMultipleSpacesByOne, removeIfEndsWith,\n}\n from './string-utils';\nimport {\n isUrl, getHostname, getParam, getPathname,\n} from './url-utils';\nimport {\n calculateAverage, roundNumber, getRandomNumber, getRandomNumbers, getPercentageDiff,\n} from './number-utils';\nimport {\n hasElementInArray, isArrayEmpty, stringToArray, getRandomItem,\n} from './array-utils';\nimport { hasUpdateReason, getStoreName } from './product.utils';\nimport { formatDate, formatMoney, formatPct } from './string-formatter';\n\nexport {\n checkIfSamePrices, findLastQuery, replaceMultipleSpacesByOne, hasMeasurementUnitMultiplier, getPriceByMeasurementUnitMultiplier,\n removeIfEndsWith, getLastAfter, includesStringArray, removeNewLines, reverseString, getParam, getPathname,\n getHostname, isUrl, isValidPrice, getMinPrice, isCheaper, getMaxPrice, getPriceOperation, getAvgPrice, calculateAverage,\n roundNumber, removeZeroPrices, isArrayEmpty, hasElementInArray, stringToArray, hasUpdateReason, getStoreName,\n formatPct, formatDate, formatMoney, getRandomNumber, getRandomNumbers, getPercentageDiff, getRandomItem,\n};\n"]}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export declare const calculateAverage: (sum: number, quantity: number) => number;
|
|
2
2
|
export declare const roundNumber: (nmb: number, decimals?: number) => number;
|
|
3
3
|
export declare const getPercentageDiff: (minPrice: number, compareToPrice: number) => number;
|
|
4
|
+
export declare const getRandomNumber: (from?: number, to?: number) => number;
|
|
5
|
+
export declare const getRandomNumbers: (from: number, to: number, qty: number, skipDuplicates?: boolean) => number[];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getPercentageDiff = exports.roundNumber = exports.calculateAverage = void 0;
|
|
3
|
+
exports.getRandomNumbers = exports.getRandomNumber = exports.getPercentageDiff = exports.roundNumber = exports.calculateAverage = void 0;
|
|
4
4
|
const lodash_1 = require("lodash");
|
|
5
5
|
exports.calculateAverage = (sum, quantity) => {
|
|
6
6
|
if (!sum || !quantity) {
|
|
@@ -29,4 +29,22 @@ exports.getPercentageDiff = (minPrice, compareToPrice) => {
|
|
|
29
29
|
}
|
|
30
30
|
return null;
|
|
31
31
|
};
|
|
32
|
+
exports.getRandomNumber = (from = 0, to = 100) => {
|
|
33
|
+
const difference = to - from;
|
|
34
|
+
let rand = Math.random();
|
|
35
|
+
rand = Math.floor(rand * difference);
|
|
36
|
+
rand += from;
|
|
37
|
+
return rand;
|
|
38
|
+
};
|
|
39
|
+
exports.getRandomNumbers = (from, to, qty, skipDuplicates = true) => {
|
|
40
|
+
const numbers = [];
|
|
41
|
+
for (let i = 0; i < qty; i++) {
|
|
42
|
+
const randomNumber = exports.getRandomNumber(from, to);
|
|
43
|
+
if (skipDuplicates && numbers.some((n) => n === randomNumber)) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
numbers.push(randomNumber);
|
|
47
|
+
}
|
|
48
|
+
return numbers;
|
|
49
|
+
};
|
|
32
50
|
//# sourceMappingURL=number-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number-utils.js","sourceRoot":"/","sources":["utils/number-utils.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAErB,QAAA,gBAAgB,GAAG,CAAC,GAAW,EAAE,QAAgB,EAAU,EAAE;IACxE,IAAI,CAAC,GAAG,IAAI,CAAC,QAAQ,EAAE;QACrB,OAAO,IAAI,CAAC;KACb;IAED,IAAI,GAAG,KAAK,CAAC,IAAI,QAAQ,KAAK,CAAC,EAAE;QAC/B,OAAO,IAAI,CAAC;KACb;IAED,OAAO,GAAG,GAAG,QAAQ,CAAC;AACxB,CAAC,CAAC;AAEW,QAAA,WAAW,GAAG,CAAC,GAAW,EAAE,QAAiB,EAAU,EAAE;IACpE,IAAI,GAAG,KAAK,CAAC,EAAE;QACb,OAAO,GAAG,CAAC;KACZ;IAED,IAAI,CAAC,GAAG,EAAE;QACR,OAAO,IAAI,CAAC;KACb;IAED,IAAI,CAAC,QAAQ,IAAI,QAAQ,GAAG,CAAC,EAAE;QAC7B,OAAO,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;KAC/B;IAED,OAAO,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;AACvC,CAAC,CAAC;AAEW,QAAA,iBAAiB,GAAG,CAAC,QAAgB,EAAE,cAAsB,EAAU,EAAE;IACpF,IAAI,iBAAQ,CAAC,QAAQ,CAAC,IAAI,iBAAQ,CAAC,cAAc,CAAC,EAAE;QAClD,OAAO,mBAAW,CAAC,GAAG,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,GAAG,cAAc,EAAE,CAAC,CAAC,CAAC;KAChE;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC","sourcesContent":["import { isNumber } from 'lodash';\n\nexport const calculateAverage = (sum: number, quantity: number): number => {\n if (!sum || !quantity) {\n return null;\n }\n\n if (sum === 0 || quantity === 0) {\n return null;\n }\n\n return sum / quantity;\n};\n\nexport const roundNumber = (nmb: number, decimals?: number): number => {\n if (nmb === 0) {\n return nmb;\n }\n\n if (!nmb) {\n return null;\n }\n\n if (!decimals || decimals < 0) {\n return Number(nmb.toFixed(0));\n }\n\n return Number(nmb.toFixed(decimals));\n};\n\nexport const getPercentageDiff = (minPrice: number, compareToPrice: number): number => {\n if (isNumber(minPrice) && isNumber(compareToPrice)) {\n return roundNumber(100 - (minPrice * 100) / compareToPrice, 2);\n }\n\n return null;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"number-utils.js","sourceRoot":"/","sources":["utils/number-utils.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAErB,QAAA,gBAAgB,GAAG,CAAC,GAAW,EAAE,QAAgB,EAAU,EAAE;IACxE,IAAI,CAAC,GAAG,IAAI,CAAC,QAAQ,EAAE;QACrB,OAAO,IAAI,CAAC;KACb;IAED,IAAI,GAAG,KAAK,CAAC,IAAI,QAAQ,KAAK,CAAC,EAAE;QAC/B,OAAO,IAAI,CAAC;KACb;IAED,OAAO,GAAG,GAAG,QAAQ,CAAC;AACxB,CAAC,CAAC;AAEW,QAAA,WAAW,GAAG,CAAC,GAAW,EAAE,QAAiB,EAAU,EAAE;IACpE,IAAI,GAAG,KAAK,CAAC,EAAE;QACb,OAAO,GAAG,CAAC;KACZ;IAED,IAAI,CAAC,GAAG,EAAE;QACR,OAAO,IAAI,CAAC;KACb;IAED,IAAI,CAAC,QAAQ,IAAI,QAAQ,GAAG,CAA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AAC,IAAI,CAAC,YAAY,CAAC,CAAC;KAC5B;IAED,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC","sourcesContent":["import { isNumber } from 'lodash';\n\nexport const calculateAverage = (sum: number, quantity: number): number => {\n if (!sum || !quantity) {\n return null;\n }\n\n if (sum === 0 || quantity === 0) {\n return null;\n }\n\n return sum / quantity;\n};\n\nexport const roundNumber = (nmb: number, decimals?: number): number => {\n if (nmb === 0) {\n return nmb;\n }\n\n if (!nmb) {\n return null;\n }\n\n if (!decimals || decimals < 0) {\n return Number(nmb.toFixed(0));\n }\n\n return Number(nmb.toFixed(decimals));\n};\n\nexport const getPercentageDiff = (minPrice: number, compareToPrice: number): number => {\n if (isNumber(minPrice) && isNumber(compareToPrice)) {\n return roundNumber(100 - (minPrice * 100) / compareToPrice, 2);\n }\n\n return null;\n};\n\nexport const getRandomNumber = (from = 0, to = 100) => {\n const difference = to - from;\n let rand = Math.random();\n rand = Math.floor(rand * difference);\n rand += from;\n\n return rand;\n};\n\nexport const getRandomNumbers = (from: number, to: number, qty: number, skipDuplicates = true): number[] => {\n const numbers: number[] = [];\n\n for (let i = 0; i < qty; i++) {\n const randomNumber = getRandomNumber(from, to);\n\n if (skipDuplicates && numbers.some((n) => n === randomNumber)) {\n continue;\n }\n\n numbers.push(randomNumber);\n }\n\n return numbers;\n};\n"]}
|
package/package.json
CHANGED
package/src/entities/product.ts
CHANGED
package/src/utils/array-utils.ts
CHANGED
|
@@ -21,3 +21,11 @@ export const stringToArray = (str: string, splitBy = ','): string[] => {
|
|
|
21
21
|
|
|
22
22
|
return str.split(splitBy).filter((e) => e);
|
|
23
23
|
};
|
|
24
|
+
|
|
25
|
+
export const getRandomItem = <T>(items: T[]): T => {
|
|
26
|
+
if (isArrayEmpty(items)) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return items[Math.floor(Math.random() * items.length)];
|
|
31
|
+
};
|
package/src/utils/index.ts
CHANGED
|
@@ -11,9 +11,11 @@ import {
|
|
|
11
11
|
isUrl, getHostname, getParam, getPathname,
|
|
12
12
|
} from './url-utils';
|
|
13
13
|
import {
|
|
14
|
-
calculateAverage, roundNumber,
|
|
14
|
+
calculateAverage, roundNumber, getRandomNumber, getRandomNumbers, getPercentageDiff,
|
|
15
15
|
} from './number-utils';
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
hasElementInArray, isArrayEmpty, stringToArray, getRandomItem,
|
|
18
|
+
} from './array-utils';
|
|
17
19
|
import { hasUpdateReason, getStoreName } from './product.utils';
|
|
18
20
|
import { formatDate, formatMoney, formatPct } from './string-formatter';
|
|
19
21
|
|
|
@@ -22,5 +24,5 @@ export {
|
|
|
22
24
|
removeIfEndsWith, getLastAfter, includesStringArray, removeNewLines, reverseString, getParam, getPathname,
|
|
23
25
|
getHostname, isUrl, isValidPrice, getMinPrice, isCheaper, getMaxPrice, getPriceOperation, getAvgPrice, calculateAverage,
|
|
24
26
|
roundNumber, removeZeroPrices, isArrayEmpty, hasElementInArray, stringToArray, hasUpdateReason, getStoreName,
|
|
25
|
-
formatPct, formatDate, formatMoney,
|
|
27
|
+
formatPct, formatDate, formatMoney, getRandomNumber, getRandomNumbers, getPercentageDiff, getRandomItem,
|
|
26
28
|
};
|
|
@@ -35,3 +35,28 @@ export const getPercentageDiff = (minPrice: number, compareToPrice: number): num
|
|
|
35
35
|
|
|
36
36
|
return null;
|
|
37
37
|
};
|
|
38
|
+
|
|
39
|
+
export const getRandomNumber = (from = 0, to = 100) => {
|
|
40
|
+
const difference = to - from;
|
|
41
|
+
let rand = Math.random();
|
|
42
|
+
rand = Math.floor(rand * difference);
|
|
43
|
+
rand += from;
|
|
44
|
+
|
|
45
|
+
return rand;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const getRandomNumbers = (from: number, to: number, qty: number, skipDuplicates = true): number[] => {
|
|
49
|
+
const numbers: number[] = [];
|
|
50
|
+
|
|
51
|
+
for (let i = 0; i < qty; i++) {
|
|
52
|
+
const randomNumber = getRandomNumber(from, to);
|
|
53
|
+
|
|
54
|
+
if (skipDuplicates && numbers.some((n) => n === randomNumber)) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
numbers.push(randomNumber);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return numbers;
|
|
62
|
+
};
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
getRandomItem, hasElementInArray, isArrayEmpty, stringToArray,
|
|
3
|
+
} from '../../src/utils/array-utils';
|
|
4
|
+
import { getRandomNumbers } from '../../src/utils/number-utils';
|
|
2
5
|
|
|
3
6
|
describe('array utils test', () => {
|
|
4
7
|
it('should return true if array contains elements', () => {
|
|
@@ -31,4 +34,19 @@ describe('array utils test', () => {
|
|
|
31
34
|
expect(stringToArray('abc', ',')).toStrictEqual(['abc']);
|
|
32
35
|
expect(stringToArray('a,b,c,d,', ',')).toStrictEqual(['a', 'b', 'c', 'd']);
|
|
33
36
|
});
|
|
37
|
+
|
|
38
|
+
it('should return null for empty array', () => {
|
|
39
|
+
expect.hasAssertions();
|
|
40
|
+
|
|
41
|
+
expect(getRandomItem([])).toBeNull();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return random item from array', () => {
|
|
45
|
+
expect.hasAssertions();
|
|
46
|
+
|
|
47
|
+
const randomNumbers = getRandomNumbers(1, 1000, 100);
|
|
48
|
+
const randomItem = getRandomItem<number>(randomNumbers);
|
|
49
|
+
|
|
50
|
+
expect(randomNumbers).toContain(randomItem);
|
|
51
|
+
});
|
|
34
52
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { calculateAverage } from '../../src';
|
|
2
|
-
import { roundNumber } from '../../src/utils/number-utils';
|
|
2
|
+
import { getRandomNumber, getRandomNumbers, roundNumber } from '../../src/utils/number-utils';
|
|
3
3
|
|
|
4
4
|
describe('number utils test', () => {
|
|
5
5
|
it('should get average between sum and quantity', () => {
|
|
@@ -24,4 +24,54 @@ describe('number utils test', () => {
|
|
|
24
24
|
expect(roundNumber(-1.445, 2)).toStrictEqual(-1.45);
|
|
25
25
|
expect(roundNumber(0, 2)).toStrictEqual(0);
|
|
26
26
|
});
|
|
27
|
+
|
|
28
|
+
it('should return random number between ranges', () => {
|
|
29
|
+
expect.hasAssertions();
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < 1000; i++) {
|
|
32
|
+
const randomNumber = getRandomNumber(i, i + 100);
|
|
33
|
+
|
|
34
|
+
expect(randomNumber).toBeDefined();
|
|
35
|
+
expect(randomNumber).toBeGreaterThanOrEqual(i);
|
|
36
|
+
expect(randomNumber).toBeLessThanOrEqual(i + 100);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should return random numbers between ranges without duplicates', () => {
|
|
41
|
+
expect.hasAssertions();
|
|
42
|
+
|
|
43
|
+
const from = 10;
|
|
44
|
+
const to = 100;
|
|
45
|
+
const qty = 1000;
|
|
46
|
+
const skipDuplicates = true;
|
|
47
|
+
|
|
48
|
+
const randomNumbers = getRandomNumbers(from, to, qty, skipDuplicates);
|
|
49
|
+
|
|
50
|
+
expect(randomNumbers).toHaveLength(new Set([...randomNumbers]).size);
|
|
51
|
+
|
|
52
|
+
randomNumbers.forEach((randomNumber) => {
|
|
53
|
+
expect(randomNumber).toBeDefined();
|
|
54
|
+
expect(randomNumber).toBeGreaterThanOrEqual(from);
|
|
55
|
+
expect(randomNumber).toBeLessThanOrEqual(to);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should return random numbers between ranges with duplicates', () => {
|
|
60
|
+
expect.hasAssertions();
|
|
61
|
+
|
|
62
|
+
const from = 10;
|
|
63
|
+
const to = 100;
|
|
64
|
+
const qty = 1000;
|
|
65
|
+
const skipDuplicates = false;
|
|
66
|
+
|
|
67
|
+
const randomNumbers = getRandomNumbers(from, to, qty, skipDuplicates);
|
|
68
|
+
|
|
69
|
+
expect(randomNumbers).toHaveLength(qty);
|
|
70
|
+
|
|
71
|
+
randomNumbers.forEach((randomNumber) => {
|
|
72
|
+
expect(randomNumber).toBeDefined();
|
|
73
|
+
expect(randomNumber).toBeGreaterThanOrEqual(from);
|
|
74
|
+
expect(randomNumber).toBeLessThanOrEqual(to);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
27
77
|
});
|