store-scrapper-js-common 1.0.164 → 1.0.165

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.
@@ -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"]}
@@ -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, };
@@ -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; } });
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"/","sources":["utils/index.ts"],"names":[],"mappings":";;;AAAA,+CAGuB;AAiBrB,kGAnBA,+BAAiB,OAmBA;AAE8B,0FArB5B,uBAAS,OAqB4B;AAAtB,4FArBJ,yBAAW,OAqBI;AAAzB,6FArBuB,0BAAY,OAqBvB;AAA0D,4FArBjC,yBAAW,OAqBiC;AAA3C,4FArBY,yBAAW,OAqBZ;AAAE,kGArBY,+BAAiB,OAqBZ;AAC3E,iGAtByF,8BAAgB,OAsBzF;AAHiC,6GAlB9D,0CAA4B,OAkB8D;AAAE,oHAlB9D,iDAAmC,OAkB8D;AAhBjI,iEAAuD;AAgBlC,8FAhBZ,oCAAa,OAgBY;AAflC,iDAGwB;AAa+C,8FAfrE,4BAAa,OAeqE;AAA7B,+FAftC,6BAAc,OAesC;AAAnC,oGAfD,kCAAmB,OAeC;AAAjC,6FAfkC,2BAAY,OAelC;AADI,2GAdgC,yCAA0B,OAchC;AAC5D,iGAf8F,+BAAgB,OAe9F;AAZlB,2CAEqB;AAWN,sFAZb,iBAAK,OAYa;AAAlB,4FAZO,uBAAW,OAYP;AADyE,yFAXhE,oBAAQ,OAWgE;AAAE,4FAXhE,uBAAW,OAWgE;AAT3G,iDAEwB;AAQiF,iGATvG,+BAAgB,OASuG;AACvH,4FAVkB,0BAAW,OAUlB;AARb,+CAA+E;AAQhC,kGARtC,+BAAiB,OAQsC;AAA/B,6FARL,0BAAY,OAQK;AAAqB,8FARxB,2BAAa,OAQwB;AAP/E,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,\n} from './number-utils';\nimport { hasElementInArray, isArrayEmpty, stringToArray } 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,\n};\n"]}
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,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;AAEW,QAAA,eAAe,GAAG,CAAC,IAAI,GAAG,CAAC,EAAE,EAAE,GAAG,GAAG,EAAE,EAAE;IACpD,MAAM,UAAU,GAAG,EAAE,GAAG,IAAI,CAAC;IAC7B,IAAI,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;IACzB,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,UAAU,CAAC,CAAC;IACrC,IAAI,IAAI,IAAI,CAAC;IAEb,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEW,QAAA,gBAAgB,GAAG,CAAC,IAAY,EAAE,EAAU,EAAE,GAAW,EAAE,cAAc,GAAG,IAAI,EAAY,EAAE;IACzG,MAAM,OAAO,GAAa,EAAE,CAAC;IAE7B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;QAC5B,MAAM,YAAY,GAAG,uBAAe,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAE/C,IAAI,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,YAAY,CAAC,EAAE;YAC7D,SAAS;SACV;QAED,OAAO,CAAC,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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "store-scrapper-js-common",
3
- "version": "1.0.164",
3
+ "version": "1.0.165",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -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
+ };
@@ -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 { hasElementInArray, isArrayEmpty, stringToArray } from './array-utils';
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 { hasElementInArray, isArrayEmpty, stringToArray } from '../../src/utils/array-utils';
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
  });