@open-tender/utils 0.3.3 → 0.3.5
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/cjs/__tests__/utils/helpers.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/helpers.test.js +35 -0
- package/dist/cjs/utils/cart.js +2 -0
- package/dist/cjs/utils/helpers.d.ts +2 -1
- package/dist/cjs/utils/helpers.js +7 -2
- package/dist/cjs/utils/theme.js +1 -1
- package/dist/esm/__tests__/utils/helpers.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/helpers.test.js +33 -0
- package/dist/esm/utils/cart.js +2 -0
- package/dist/esm/utils/helpers.d.ts +2 -1
- package/dist/esm/utils/helpers.js +5 -1
- package/dist/esm/utils/theme.js +1 -1
- package/package.json +7 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("../../utils");
|
|
4
|
+
describe('formatPercentage', () => {
|
|
5
|
+
it('formats percentage when decimal is undefined', () => {
|
|
6
|
+
expect((0, utils_1.formatPercentage)('0')).toBe('0');
|
|
7
|
+
expect((0, utils_1.formatPercentage)('0.45')).toBe('0.45');
|
|
8
|
+
expect((0, utils_1.formatPercentage)('0.5')).toBe('0.5');
|
|
9
|
+
expect((0, utils_1.formatPercentage)('1')).toBe('1');
|
|
10
|
+
expect((0, utils_1.formatPercentage)('1.55')).toBe('1.55');
|
|
11
|
+
});
|
|
12
|
+
it('formats percentage with 0 decimal points', () => {
|
|
13
|
+
expect((0, utils_1.formatPercentage)('0', 0)).toBe('0');
|
|
14
|
+
expect((0, utils_1.formatPercentage)('0.45', 0)).toBe('0');
|
|
15
|
+
expect((0, utils_1.formatPercentage)('0.5', 0)).toBe('1');
|
|
16
|
+
expect((0, utils_1.formatPercentage)('1', 0)).toBe('1');
|
|
17
|
+
expect((0, utils_1.formatPercentage)('1.55', 0)).toBe('2');
|
|
18
|
+
});
|
|
19
|
+
it('formats percentage with 2 decimal points', () => {
|
|
20
|
+
expect((0, utils_1.formatPercentage)('0', 2)).toBe('0.00');
|
|
21
|
+
expect((0, utils_1.formatPercentage)('0.45', 2)).toBe('0.45');
|
|
22
|
+
expect((0, utils_1.formatPercentage)('0.5', 2)).toBe('0.50');
|
|
23
|
+
expect((0, utils_1.formatPercentage)('1', 2)).toBe('1.00');
|
|
24
|
+
expect((0, utils_1.formatPercentage)('1.55', 2)).toBe('1.55');
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe('formatQuantity', () => {
|
|
28
|
+
it('formats quantity', () => {
|
|
29
|
+
expect((0, utils_1.formatQuantity)('0')).toBe('0');
|
|
30
|
+
expect((0, utils_1.formatQuantity)('0.45')).toBe('0');
|
|
31
|
+
expect((0, utils_1.formatQuantity)('0.5')).toBe('1');
|
|
32
|
+
expect((0, utils_1.formatQuantity)('1')).toBe('1');
|
|
33
|
+
expect((0, utils_1.formatQuantity)('1.55')).toBe('2');
|
|
34
|
+
});
|
|
35
|
+
});
|
package/dist/cjs/utils/cart.js
CHANGED
|
@@ -37,8 +37,9 @@ export declare const makeImageMap: (images: Images) => {
|
|
|
37
37
|
export declare const makeImageUrl: (images: ContentImages, isMobile: boolean) => string | null | undefined;
|
|
38
38
|
export declare const makeSlides: (items: Announcements, isMobile: boolean) => Announcements | null;
|
|
39
39
|
export declare const displayPrice: (price: string) => string;
|
|
40
|
-
export declare const addCommas: (x: number, d
|
|
40
|
+
export declare const addCommas: (x: number, d?: number) => string;
|
|
41
41
|
export declare const formatQuantity: (n: string) => string;
|
|
42
|
+
export declare const formatPercentage: (n: string, d?: number) => string;
|
|
42
43
|
export declare const formatDollars: (str: string, space?: string, decimals?: number) => string;
|
|
43
44
|
export declare const convertStringToArray: (str: string | null) => string[];
|
|
44
45
|
export declare const hasEntities: <T extends unknown[]>(reducer: {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasEntities = exports.convertStringToArray = exports.formatDollars = exports.formatQuantity = exports.addCommas = exports.displayPrice = exports.makeSlides = exports.makeImageUrl = exports.makeImageMap = exports.getHeight = exports.getWidth = exports.makeRandomNumberString = exports.checkNotFound = exports.checkGuestData = exports.checkEmpty = exports.validateEmail = exports.dashesToSlashes = exports.slashesToDashes = exports.makeBirthDate = exports.validatePhone = exports.makePhone = exports.cleanPhone = exports.capitalize = exports.serialize = exports.stripTags = exports.slugify = exports.contains = exports.isNum = exports.isEmpty = exports.isObject = exports.isString = void 0;
|
|
3
|
+
exports.hasEntities = exports.convertStringToArray = exports.formatDollars = exports.formatPercentage = exports.formatQuantity = exports.addCommas = exports.displayPrice = exports.makeSlides = exports.makeImageUrl = exports.makeImageMap = exports.getHeight = exports.getWidth = exports.makeRandomNumberString = exports.checkNotFound = exports.checkGuestData = exports.checkEmpty = exports.validateEmail = exports.dashesToSlashes = exports.slashesToDashes = exports.makeBirthDate = exports.validatePhone = exports.makePhone = exports.cleanPhone = exports.capitalize = exports.serialize = exports.stripTags = exports.slugify = exports.contains = exports.isNum = exports.isEmpty = exports.isObject = exports.isString = void 0;
|
|
4
4
|
const isString = (str) => {
|
|
5
5
|
return typeof str === 'string';
|
|
6
6
|
};
|
|
@@ -169,13 +169,18 @@ const displayPrice = (price) => {
|
|
|
169
169
|
};
|
|
170
170
|
exports.displayPrice = displayPrice;
|
|
171
171
|
const addCommas = (x, d) => {
|
|
172
|
-
|
|
172
|
+
const valueToReplace = d === undefined ? x.toString() : x.toFixed(d);
|
|
173
|
+
return valueToReplace.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
173
174
|
};
|
|
174
175
|
exports.addCommas = addCommas;
|
|
175
176
|
const formatQuantity = (n) => {
|
|
176
177
|
return (0, exports.addCommas)(parseFloat(n), 0);
|
|
177
178
|
};
|
|
178
179
|
exports.formatQuantity = formatQuantity;
|
|
180
|
+
const formatPercentage = (n, d) => {
|
|
181
|
+
return (0, exports.addCommas)(parseFloat(n), d);
|
|
182
|
+
};
|
|
183
|
+
exports.formatPercentage = formatPercentage;
|
|
179
184
|
const formatDollars = (str, space = '', decimals = 2) => {
|
|
180
185
|
const floatPrice = parseFloat(str);
|
|
181
186
|
return floatPrice < 0
|
package/dist/cjs/utils/theme.js
CHANGED
|
@@ -178,7 +178,7 @@ const makeMenuItems = (menuItems, lineHeight) => {
|
|
|
178
178
|
calories: Object.assign(Object.assign({}, calories), { fontSize: (0, exports.remsToPixels)(calories.fontSize), fontSizeMobile: (0, exports.remsToPixels)(calories.fontSizeMobile), lineHeight: fontSizeToLineHeight(lineHeight, calories.fontSizeMobile), padding: boxRemsToPixels(calories.padding), paddingMobile: boxRemsToPixels(calories.paddingMobile) }),
|
|
179
179
|
container: Object.assign(Object.assign({}, container), { gap: (0, exports.remsToPixels)(container.gap), gapHalf: (0, exports.remsToPixels)(container.gapHalf), gapHalfMobile: (0, exports.remsToPixels)(container.gapHalfMobile), gapMobile: (0, exports.remsToPixels)(container.gapMobile), margin: boxRemsToPixels(container.margin), marginMobile: boxRemsToPixels(container.marginMobile), maxWidth: (0, exports.remsToPixels)(container.maxWidth), padding: boxRemsToPixels(container.padding), paddingMobile: boxRemsToPixels(container.paddingMobile) }),
|
|
180
180
|
content: Object.assign(Object.assign({}, content), { padding: boxRemsToPixels(content.padding), paddingMobile: boxRemsToPixels(content.paddingMobile) }),
|
|
181
|
-
buttons: Object.assign(Object.assign({}, buttons), { padding: boxRemsToPixels(buttons.padding), paddingMobile: boxRemsToPixels(buttons.paddingMobile) }),
|
|
181
|
+
buttons: Object.assign(Object.assign({}, buttons), { gap: (0, exports.remsToPixels)(buttons.gap), gapMobile: (0, exports.remsToPixels)(buttons.gapMobile), padding: boxRemsToPixels(buttons.padding), paddingMobile: boxRemsToPixels(buttons.paddingMobile) }),
|
|
182
182
|
image: Object.assign(Object.assign({}, image), { border: boxRemsToPixels(image.border), borderMobile: boxRemsToPixels(image.borderMobile), borderRadius: (0, exports.remsToPixels)(image.borderRadius), borderRadiusMobile: (0, exports.remsToPixels)(image.borderRadiusMobile), margin: boxRemsToPixels(image.margin), marginMobile: boxRemsToPixels(image.marginMobile), minWidth: (0, exports.remsToPixels)(image.minWidth), minWidthMobile: (0, exports.remsToPixels)(image.minWidthMobile), padding: boxRemsToPixels(image.padding), paddingMobile: boxRemsToPixels(image.paddingMobile) }),
|
|
183
183
|
price: Object.assign(Object.assign({}, price), { fontSize: (0, exports.remsToPixels)(price.fontSize), fontSizeMobile: (0, exports.remsToPixels)(price.fontSizeMobile), lineHeight: fontSizeToLineHeight(lineHeight, price.fontSizeMobile), padding: boxRemsToPixels(price.padding), paddingMobile: boxRemsToPixels(price.paddingMobile) }),
|
|
184
184
|
tags: Object.assign(Object.assign({}, tags), { fontSize: (0, exports.remsToPixels)(tags.fontSize), fontSizeMobile: (0, exports.remsToPixels)(tags.fontSizeMobile), letterSpacing: (0, exports.remsToPixels)(tags.letterSpacing), lineHeight: fontSizeToLineHeight(lineHeight, tags.fontSizeMobile), gap: (0, exports.remsToPixels)(tags.gap), gapMobile: (0, exports.remsToPixels)(tags.gapMobile), width: (0, exports.remsToPixels)(tags.width), widthMobile: (0, exports.remsToPixels)(tags.widthMobile) }),
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { formatPercentage, formatQuantity } from "../../utils";
|
|
2
|
+
describe('formatPercentage', () => {
|
|
3
|
+
it('formats percentage when decimal is undefined', () => {
|
|
4
|
+
expect(formatPercentage('0')).toBe('0');
|
|
5
|
+
expect(formatPercentage('0.45')).toBe('0.45');
|
|
6
|
+
expect(formatPercentage('0.5')).toBe('0.5');
|
|
7
|
+
expect(formatPercentage('1')).toBe('1');
|
|
8
|
+
expect(formatPercentage('1.55')).toBe('1.55');
|
|
9
|
+
});
|
|
10
|
+
it('formats percentage with 0 decimal points', () => {
|
|
11
|
+
expect(formatPercentage('0', 0)).toBe('0');
|
|
12
|
+
expect(formatPercentage('0.45', 0)).toBe('0');
|
|
13
|
+
expect(formatPercentage('0.5', 0)).toBe('1');
|
|
14
|
+
expect(formatPercentage('1', 0)).toBe('1');
|
|
15
|
+
expect(formatPercentage('1.55', 0)).toBe('2');
|
|
16
|
+
});
|
|
17
|
+
it('formats percentage with 2 decimal points', () => {
|
|
18
|
+
expect(formatPercentage('0', 2)).toBe('0.00');
|
|
19
|
+
expect(formatPercentage('0.45', 2)).toBe('0.45');
|
|
20
|
+
expect(formatPercentage('0.5', 2)).toBe('0.50');
|
|
21
|
+
expect(formatPercentage('1', 2)).toBe('1.00');
|
|
22
|
+
expect(formatPercentage('1.55', 2)).toBe('1.55');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
describe('formatQuantity', () => {
|
|
26
|
+
it('formats quantity', () => {
|
|
27
|
+
expect(formatQuantity('0')).toBe('0');
|
|
28
|
+
expect(formatQuantity('0.45')).toBe('0');
|
|
29
|
+
expect(formatQuantity('0.5')).toBe('1');
|
|
30
|
+
expect(formatQuantity('1')).toBe('1');
|
|
31
|
+
expect(formatQuantity('1.55')).toBe('2');
|
|
32
|
+
});
|
|
33
|
+
});
|
package/dist/esm/utils/cart.js
CHANGED
|
@@ -37,8 +37,9 @@ export declare const makeImageMap: (images: Images) => {
|
|
|
37
37
|
export declare const makeImageUrl: (images: ContentImages, isMobile: boolean) => string | null | undefined;
|
|
38
38
|
export declare const makeSlides: (items: Announcements, isMobile: boolean) => Announcements | null;
|
|
39
39
|
export declare const displayPrice: (price: string) => string;
|
|
40
|
-
export declare const addCommas: (x: number, d
|
|
40
|
+
export declare const addCommas: (x: number, d?: number) => string;
|
|
41
41
|
export declare const formatQuantity: (n: string) => string;
|
|
42
|
+
export declare const formatPercentage: (n: string, d?: number) => string;
|
|
42
43
|
export declare const formatDollars: (str: string, space?: string, decimals?: number) => string;
|
|
43
44
|
export declare const convertStringToArray: (str: string | null) => string[];
|
|
44
45
|
export declare const hasEntities: <T extends unknown[]>(reducer: {
|
|
@@ -140,11 +140,15 @@ export const displayPrice = (price) => {
|
|
|
140
140
|
return parseFloat(price).toFixed(2);
|
|
141
141
|
};
|
|
142
142
|
export const addCommas = (x, d) => {
|
|
143
|
-
|
|
143
|
+
const valueToReplace = d === undefined ? x.toString() : x.toFixed(d);
|
|
144
|
+
return valueToReplace.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
144
145
|
};
|
|
145
146
|
export const formatQuantity = (n) => {
|
|
146
147
|
return addCommas(parseFloat(n), 0);
|
|
147
148
|
};
|
|
149
|
+
export const formatPercentage = (n, d) => {
|
|
150
|
+
return addCommas(parseFloat(n), d);
|
|
151
|
+
};
|
|
148
152
|
export const formatDollars = (str, space = '', decimals = 2) => {
|
|
149
153
|
const floatPrice = parseFloat(str);
|
|
150
154
|
return floatPrice < 0
|
package/dist/esm/utils/theme.js
CHANGED
|
@@ -169,7 +169,7 @@ const makeMenuItems = (menuItems, lineHeight) => {
|
|
|
169
169
|
calories: Object.assign(Object.assign({}, calories), { fontSize: remsToPixels(calories.fontSize), fontSizeMobile: remsToPixels(calories.fontSizeMobile), lineHeight: fontSizeToLineHeight(lineHeight, calories.fontSizeMobile), padding: boxRemsToPixels(calories.padding), paddingMobile: boxRemsToPixels(calories.paddingMobile) }),
|
|
170
170
|
container: Object.assign(Object.assign({}, container), { gap: remsToPixels(container.gap), gapHalf: remsToPixels(container.gapHalf), gapHalfMobile: remsToPixels(container.gapHalfMobile), gapMobile: remsToPixels(container.gapMobile), margin: boxRemsToPixels(container.margin), marginMobile: boxRemsToPixels(container.marginMobile), maxWidth: remsToPixels(container.maxWidth), padding: boxRemsToPixels(container.padding), paddingMobile: boxRemsToPixels(container.paddingMobile) }),
|
|
171
171
|
content: Object.assign(Object.assign({}, content), { padding: boxRemsToPixels(content.padding), paddingMobile: boxRemsToPixels(content.paddingMobile) }),
|
|
172
|
-
buttons: Object.assign(Object.assign({}, buttons), { padding: boxRemsToPixels(buttons.padding), paddingMobile: boxRemsToPixels(buttons.paddingMobile) }),
|
|
172
|
+
buttons: Object.assign(Object.assign({}, buttons), { gap: remsToPixels(buttons.gap), gapMobile: remsToPixels(buttons.gapMobile), padding: boxRemsToPixels(buttons.padding), paddingMobile: boxRemsToPixels(buttons.paddingMobile) }),
|
|
173
173
|
image: Object.assign(Object.assign({}, image), { border: boxRemsToPixels(image.border), borderMobile: boxRemsToPixels(image.borderMobile), borderRadius: remsToPixels(image.borderRadius), borderRadiusMobile: remsToPixels(image.borderRadiusMobile), margin: boxRemsToPixels(image.margin), marginMobile: boxRemsToPixels(image.marginMobile), minWidth: remsToPixels(image.minWidth), minWidthMobile: remsToPixels(image.minWidthMobile), padding: boxRemsToPixels(image.padding), paddingMobile: boxRemsToPixels(image.paddingMobile) }),
|
|
174
174
|
price: Object.assign(Object.assign({}, price), { fontSize: remsToPixels(price.fontSize), fontSizeMobile: remsToPixels(price.fontSizeMobile), lineHeight: fontSizeToLineHeight(lineHeight, price.fontSizeMobile), padding: boxRemsToPixels(price.padding), paddingMobile: boxRemsToPixels(price.paddingMobile) }),
|
|
175
175
|
tags: Object.assign(Object.assign({}, tags), { fontSize: remsToPixels(tags.fontSize), fontSizeMobile: remsToPixels(tags.fontSizeMobile), letterSpacing: remsToPixels(tags.letterSpacing), lineHeight: fontSizeToLineHeight(lineHeight, tags.fontSizeMobile), gap: remsToPixels(tags.gap), gapMobile: remsToPixels(tags.gapMobile), width: remsToPixels(tags.width), widthMobile: remsToPixels(tags.widthMobile) }),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-tender/utils",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"build:esm": "tsc",
|
|
14
14
|
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
|
|
15
15
|
"lint": "eslint \"{**/*,*}.{js,ts,jsx,tsx}\"",
|
|
16
|
-
"prettier": "prettier --write \"{src,tests,example/src}/**/*.{js,ts,jsx,tsx}\""
|
|
16
|
+
"prettier": "prettier --write \"{src,tests,example/src}/**/*.{js,ts,jsx,tsx}\"",
|
|
17
|
+
"test": "jest"
|
|
17
18
|
},
|
|
18
19
|
"homepage": "https://github.com/open-tender/open-tender-utils",
|
|
19
20
|
"bugs": {
|
|
@@ -34,10 +35,10 @@
|
|
|
34
35
|
"license": "MIT",
|
|
35
36
|
"private": false,
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"@babel/core": "^7.19.1",
|
|
38
38
|
"@emotion/react": "^11.10.4",
|
|
39
39
|
"@reduxjs/toolkit": "^1.8.5",
|
|
40
40
|
"@types/google.maps": "^3.53.5",
|
|
41
|
+
"@types/jest": "^29.5.11",
|
|
41
42
|
"@types/react": "^18.0.18",
|
|
42
43
|
"@types/react-dom": "^18.0.6",
|
|
43
44
|
"@types/uuid": "^8.3.4",
|
|
@@ -48,13 +49,15 @@
|
|
|
48
49
|
"eslint-plugin-prettier": "^4.2.1",
|
|
49
50
|
"eslint-plugin-react": "^7.31.7",
|
|
50
51
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
52
|
+
"jest": "^29.7.0",
|
|
51
53
|
"prettier": "^2.7.1",
|
|
52
54
|
"react": "^18.2.0",
|
|
53
55
|
"react-redux": "^8.0.2",
|
|
56
|
+
"ts-jest": "^29.1.1",
|
|
54
57
|
"typescript": "^4.8.2"
|
|
55
58
|
},
|
|
56
59
|
"peerDependencies": {
|
|
57
|
-
"@open-tender/types": "^0.3.
|
|
60
|
+
"@open-tender/types": "^0.3.10",
|
|
58
61
|
"@reduxjs/toolkit": "^1.8.5",
|
|
59
62
|
"react": "^18.2.0"
|
|
60
63
|
},
|