@open-tender/utils 0.3.4 → 0.3.6
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/__tests__/utils/theme.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/theme.test.js +37 -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 +23 -7
- package/dist/esm/__tests__/utils/helpers.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/helpers.test.js +33 -0
- package/dist/esm/__tests__/utils/theme.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/theme.test.js +35 -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 +23 -7
- package/package.json +7 -3
- package/dist/.DS_Store +0 -0
|
@@ -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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("../../utils");
|
|
4
|
+
describe('remsToPixels', () => {
|
|
5
|
+
it('should return 0 if rems is undefined', () => {
|
|
6
|
+
expect((0, utils_1.remsToPixels)()).toBe('0');
|
|
7
|
+
});
|
|
8
|
+
it('should return 0 without unit if rems is 0', () => {
|
|
9
|
+
expect((0, utils_1.remsToPixels)('0')).toBe('0');
|
|
10
|
+
});
|
|
11
|
+
it('should convert rems to pixels for single values without unit', () => {
|
|
12
|
+
expect((0, utils_1.remsToPixels)('1')).toBe('10px');
|
|
13
|
+
expect((0, utils_1.remsToPixels)('1.5')).toBe('15px');
|
|
14
|
+
expect((0, utils_1.remsToPixels)('2')).toBe('20px');
|
|
15
|
+
expect((0, utils_1.remsToPixels)('2.5')).toBe('25px');
|
|
16
|
+
});
|
|
17
|
+
it('should convert rems to pixels for single values', () => {
|
|
18
|
+
expect((0, utils_1.remsToPixels)('1rem')).toBe('10px');
|
|
19
|
+
expect((0, utils_1.remsToPixels)('1.5rem')).toBe('15px');
|
|
20
|
+
expect((0, utils_1.remsToPixels)('2rem')).toBe('20px');
|
|
21
|
+
expect((0, utils_1.remsToPixels)('2.5rem')).toBe('25px');
|
|
22
|
+
});
|
|
23
|
+
it('should preseve negative values', () => {
|
|
24
|
+
expect((0, utils_1.remsToPixels)('-1rem')).toBe('-10px');
|
|
25
|
+
expect((0, utils_1.remsToPixels)('-1.5rem')).toBe('-15px');
|
|
26
|
+
});
|
|
27
|
+
it('should convert multiple values rems to pixels', () => {
|
|
28
|
+
expect((0, utils_1.remsToPixels)('1rem 2rem')).toBe('10px 20px');
|
|
29
|
+
expect((0, utils_1.remsToPixels)('1rem 2rem 3rem')).toBe('10px 20px 30px');
|
|
30
|
+
expect((0, utils_1.remsToPixels)('1rem 2rem 3rem 4rem')).toBe('10px 20px 30px 40px');
|
|
31
|
+
});
|
|
32
|
+
it('should ignore whitespaces around the input', () => {
|
|
33
|
+
expect((0, utils_1.remsToPixels)(' ')).toBe('0');
|
|
34
|
+
expect((0, utils_1.remsToPixels)(' 1.5rem ')).toBe('15px');
|
|
35
|
+
expect((0, utils_1.remsToPixels)(' 1.5rem 2rem')).toBe('15px 20px');
|
|
36
|
+
});
|
|
37
|
+
});
|
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
|
@@ -7,13 +7,21 @@ const makeLineHeight = (lineHeight, fontSize) => {
|
|
|
7
7
|
return `${(parseFloat(lineHeight !== null && lineHeight !== void 0 ? lineHeight : '0') * size).toFixed(3)}px`;
|
|
8
8
|
};
|
|
9
9
|
exports.makeLineHeight = makeLineHeight;
|
|
10
|
+
// TODO: remove responsive option
|
|
10
11
|
const remsToPixels = (rems, responsive = false) => {
|
|
11
|
-
|
|
12
|
+
const cleanedUpRems = rems === null || rems === void 0 ? void 0 : rems.trim().replace(/\s+/g, ' ');
|
|
13
|
+
if (!cleanedUpRems)
|
|
12
14
|
return '0';
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
const remsToPixelsSingleValue = (remsValue) => {
|
|
16
|
+
const pixels = parseFloat(remsValue.replace('rem', '')) * 10;
|
|
17
|
+
const adjustedPixels = responsive ? pixels : pixels;
|
|
18
|
+
const noZeroes = parseFloat(adjustedPixels.toFixed(2)).toString();
|
|
19
|
+
return noZeroes === '0' ? '0' : `${noZeroes}px`;
|
|
20
|
+
};
|
|
21
|
+
return cleanedUpRems
|
|
22
|
+
.split(' ')
|
|
23
|
+
.map(i => remsToPixelsSingleValue(i))
|
|
24
|
+
.join(' ');
|
|
17
25
|
};
|
|
18
26
|
exports.remsToPixels = remsToPixels;
|
|
19
27
|
const remsToNumber = (rems) => {
|
|
@@ -48,6 +56,9 @@ const paddingToPixels = (padding) => {
|
|
|
48
56
|
const [top, horizontal, bottom] = padding.split(' ');
|
|
49
57
|
return `${(0, exports.remsToPixels)(top)} ${(0, exports.remsToPixels)(horizontal)} ${(0, exports.remsToPixels)(bottom)}`;
|
|
50
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* @deprecated use remsToPixels instead
|
|
61
|
+
*/
|
|
51
62
|
const boxRemsToPixels = (box) => {
|
|
52
63
|
const pixels = box
|
|
53
64
|
.split(' ')
|
|
@@ -178,7 +189,7 @@ const makeMenuItems = (menuItems, lineHeight) => {
|
|
|
178
189
|
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
190
|
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
191
|
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) }),
|
|
192
|
+
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
193
|
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
194
|
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
195
|
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) }),
|
|
@@ -191,6 +202,9 @@ const makeModifiers = (modifiers, lineHeight) => {
|
|
|
191
202
|
const { box, calories, content, image, price, text, title } = modifiers;
|
|
192
203
|
return Object.assign(Object.assign({}, modifiers), { box: Object.assign(Object.assign({}, box), { border: boxRemsToPixels(box.border), borderMobile: boxRemsToPixels(box.borderMobile), borderRadius: (0, exports.remsToPixels)(box.borderRadius), gap: (0, exports.remsToPixels)(box.gap), gapHalf: (0, exports.remsToPixels)(box.gapHalf), margin: boxRemsToPixels(box.margin), marginMobile: boxRemsToPixels(box.marginMobile), minHeight: (0, exports.remsToPixels)(box.minHeight), minHeightWithGap: (0, exports.remsToPixels)(box.minHeightWithGap), minWidth: (0, exports.remsToPixels)(box.minWidth), padding: boxRemsToPixels(box.padding), paddingMobile: boxRemsToPixels(box.paddingMobile) }), calories: Object.assign(Object.assign({}, calories), { fontSize: (0, exports.remsToPixels)(calories.fontSize), lineHeight: fontSizeToLineHeight(calories.fontSize, lineHeight) }), content: Object.assign(Object.assign({}, content), { padding: boxRemsToPixels(content.padding) }), image: Object.assign(Object.assign({}, image), { border: boxRemsToPixels(image.border), borderRadius: (0, exports.remsToPixels)(image.borderRadius), height: (0, exports.remsToPixels)(image.height), margin: boxRemsToPixels(image.margin), padding: boxRemsToPixels(image.padding), width: (0, exports.remsToPixels)(image.width) }), price: Object.assign(Object.assign({}, price), { fontSize: (0, exports.remsToPixels)(price.fontSize), lineHeight: fontSizeToLineHeight(price.fontSize, lineHeight) }), text: Object.assign(Object.assign({}, text), { fontSize: (0, exports.remsToPixels)(text.fontSize), lineHeight: fontSizeToLineHeight(text.fontSize, lineHeight) }), title: Object.assign(Object.assign({}, title), { fontSize: (0, exports.remsToPixels)(title.fontSize), lineHeight: fontSizeToLineHeight(title.fontSize, lineHeight) }) });
|
|
193
204
|
};
|
|
205
|
+
const makeTags = (tags) => {
|
|
206
|
+
return Object.assign(Object.assign({}, tags), { image: Object.assign(Object.assign({}, tags.image), { padding: (0, exports.remsToPixels)(tags.container.padding), paddingMobile: (0, exports.remsToPixels)(tags.container.paddingMobile), margin: (0, exports.remsToPixels)(tags.container.margin), marginMobile: (0, exports.remsToPixels)(tags.container.marginMobile) }), container: Object.assign(Object.assign({}, tags.container), { padding: (0, exports.remsToPixels)(tags.container.padding), paddingMobile: (0, exports.remsToPixels)(tags.container.paddingMobile), margin: (0, exports.remsToPixels)(tags.container.margin), marginMobile: (0, exports.remsToPixels)(tags.container.marginMobile) }), box: Object.assign(Object.assign({}, tags.box), { padding: (0, exports.remsToPixels)(tags.box.padding), paddingMobile: (0, exports.remsToPixels)(tags.box.paddingMobile), margin: (0, exports.remsToPixels)(tags.box.margin), marginMobile: (0, exports.remsToPixels)(tags.box.marginMobile) }), text: Object.assign(Object.assign({}, tags.text), { fontSize: (0, exports.remsToPixels)(tags.text.fontSize), fontSizeMobile: (0, exports.remsToPixels)(tags.text.fontSizeMobile) }) });
|
|
207
|
+
};
|
|
194
208
|
const makeLogo = (logo) => {
|
|
195
209
|
return {
|
|
196
210
|
marginTop: (0, exports.remsToPixels)(logo.marginTop),
|
|
@@ -215,11 +229,13 @@ const decorateTheme = (theme) => {
|
|
|
215
229
|
const categories = makeCategories(theme.categories);
|
|
216
230
|
const menuItems = makeMenuItems(theme.menuItems, theme.lineHeight);
|
|
217
231
|
const modifiers = makeModifiers(theme.modifiers, theme.lineHeight);
|
|
232
|
+
const tags = makeTags(theme.tags);
|
|
218
233
|
return Object.assign(Object.assign({}, theme), { logo,
|
|
219
234
|
logoFooter, fonts: fonts, lineHeight, layout: layout, buttons: buttons, border, inputs: inputs, counts,
|
|
220
235
|
cards, welcome: welcome, item: item, categories,
|
|
221
236
|
menuItems,
|
|
222
|
-
modifiers
|
|
237
|
+
modifiers,
|
|
238
|
+
tags });
|
|
223
239
|
};
|
|
224
240
|
exports.decorateTheme = decorateTheme;
|
|
225
241
|
const hexToRgbA = (hex, opacity) => {
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { remsToPixels } from '../../utils';
|
|
2
|
+
describe('remsToPixels', () => {
|
|
3
|
+
it('should return 0 if rems is undefined', () => {
|
|
4
|
+
expect(remsToPixels()).toBe('0');
|
|
5
|
+
});
|
|
6
|
+
it('should return 0 without unit if rems is 0', () => {
|
|
7
|
+
expect(remsToPixels('0')).toBe('0');
|
|
8
|
+
});
|
|
9
|
+
it('should convert rems to pixels for single values without unit', () => {
|
|
10
|
+
expect(remsToPixels('1')).toBe('10px');
|
|
11
|
+
expect(remsToPixels('1.5')).toBe('15px');
|
|
12
|
+
expect(remsToPixels('2')).toBe('20px');
|
|
13
|
+
expect(remsToPixels('2.5')).toBe('25px');
|
|
14
|
+
});
|
|
15
|
+
it('should convert rems to pixels for single values', () => {
|
|
16
|
+
expect(remsToPixels('1rem')).toBe('10px');
|
|
17
|
+
expect(remsToPixels('1.5rem')).toBe('15px');
|
|
18
|
+
expect(remsToPixels('2rem')).toBe('20px');
|
|
19
|
+
expect(remsToPixels('2.5rem')).toBe('25px');
|
|
20
|
+
});
|
|
21
|
+
it('should preseve negative values', () => {
|
|
22
|
+
expect(remsToPixels('-1rem')).toBe('-10px');
|
|
23
|
+
expect(remsToPixels('-1.5rem')).toBe('-15px');
|
|
24
|
+
});
|
|
25
|
+
it('should convert multiple values rems to pixels', () => {
|
|
26
|
+
expect(remsToPixels('1rem 2rem')).toBe('10px 20px');
|
|
27
|
+
expect(remsToPixels('1rem 2rem 3rem')).toBe('10px 20px 30px');
|
|
28
|
+
expect(remsToPixels('1rem 2rem 3rem 4rem')).toBe('10px 20px 30px 40px');
|
|
29
|
+
});
|
|
30
|
+
it('should ignore whitespaces around the input', () => {
|
|
31
|
+
expect(remsToPixels(' ')).toBe('0');
|
|
32
|
+
expect(remsToPixels(' 1.5rem ')).toBe('15px');
|
|
33
|
+
expect(remsToPixels(' 1.5rem 2rem')).toBe('15px 20px');
|
|
34
|
+
});
|
|
35
|
+
});
|
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
|
@@ -3,13 +3,21 @@ export const makeLineHeight = (lineHeight, fontSize) => {
|
|
|
3
3
|
const size = parseInt((_a = fontSize === null || fontSize === void 0 ? void 0 : fontSize.replace('px', '')) !== null && _a !== void 0 ? _a : '0');
|
|
4
4
|
return `${(parseFloat(lineHeight !== null && lineHeight !== void 0 ? lineHeight : '0') * size).toFixed(3)}px`;
|
|
5
5
|
};
|
|
6
|
+
// TODO: remove responsive option
|
|
6
7
|
export const remsToPixels = (rems, responsive = false) => {
|
|
7
|
-
|
|
8
|
+
const cleanedUpRems = rems === null || rems === void 0 ? void 0 : rems.trim().replace(/\s+/g, ' ');
|
|
9
|
+
if (!cleanedUpRems)
|
|
8
10
|
return '0';
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const remsToPixelsSingleValue = (remsValue) => {
|
|
12
|
+
const pixels = parseFloat(remsValue.replace('rem', '')) * 10;
|
|
13
|
+
const adjustedPixels = responsive ? pixels : pixels;
|
|
14
|
+
const noZeroes = parseFloat(adjustedPixels.toFixed(2)).toString();
|
|
15
|
+
return noZeroes === '0' ? '0' : `${noZeroes}px`;
|
|
16
|
+
};
|
|
17
|
+
return cleanedUpRems
|
|
18
|
+
.split(' ')
|
|
19
|
+
.map(i => remsToPixelsSingleValue(i))
|
|
20
|
+
.join(' ');
|
|
13
21
|
};
|
|
14
22
|
export const remsToNumber = (rems) => {
|
|
15
23
|
return parseFloat(rems.replace('rem', '')) * 10;
|
|
@@ -39,6 +47,9 @@ const paddingToPixels = (padding) => {
|
|
|
39
47
|
const [top, horizontal, bottom] = padding.split(' ');
|
|
40
48
|
return `${remsToPixels(top)} ${remsToPixels(horizontal)} ${remsToPixels(bottom)}`;
|
|
41
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated use remsToPixels instead
|
|
52
|
+
*/
|
|
42
53
|
const boxRemsToPixels = (box) => {
|
|
43
54
|
const pixels = box
|
|
44
55
|
.split(' ')
|
|
@@ -169,7 +180,7 @@ const makeMenuItems = (menuItems, lineHeight) => {
|
|
|
169
180
|
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
181
|
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
182
|
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) }),
|
|
183
|
+
buttons: Object.assign(Object.assign({}, buttons), { gap: remsToPixels(buttons.gap), gapMobile: remsToPixels(buttons.gapMobile), padding: boxRemsToPixels(buttons.padding), paddingMobile: boxRemsToPixels(buttons.paddingMobile) }),
|
|
173
184
|
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
185
|
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
186
|
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) }),
|
|
@@ -182,6 +193,9 @@ const makeModifiers = (modifiers, lineHeight) => {
|
|
|
182
193
|
const { box, calories, content, image, price, text, title } = modifiers;
|
|
183
194
|
return Object.assign(Object.assign({}, modifiers), { box: Object.assign(Object.assign({}, box), { border: boxRemsToPixels(box.border), borderMobile: boxRemsToPixels(box.borderMobile), borderRadius: remsToPixels(box.borderRadius), gap: remsToPixels(box.gap), gapHalf: remsToPixels(box.gapHalf), margin: boxRemsToPixels(box.margin), marginMobile: boxRemsToPixels(box.marginMobile), minHeight: remsToPixels(box.minHeight), minHeightWithGap: remsToPixels(box.minHeightWithGap), minWidth: remsToPixels(box.minWidth), padding: boxRemsToPixels(box.padding), paddingMobile: boxRemsToPixels(box.paddingMobile) }), calories: Object.assign(Object.assign({}, calories), { fontSize: remsToPixels(calories.fontSize), lineHeight: fontSizeToLineHeight(calories.fontSize, lineHeight) }), content: Object.assign(Object.assign({}, content), { padding: boxRemsToPixels(content.padding) }), image: Object.assign(Object.assign({}, image), { border: boxRemsToPixels(image.border), borderRadius: remsToPixels(image.borderRadius), height: remsToPixels(image.height), margin: boxRemsToPixels(image.margin), padding: boxRemsToPixels(image.padding), width: remsToPixels(image.width) }), price: Object.assign(Object.assign({}, price), { fontSize: remsToPixels(price.fontSize), lineHeight: fontSizeToLineHeight(price.fontSize, lineHeight) }), text: Object.assign(Object.assign({}, text), { fontSize: remsToPixels(text.fontSize), lineHeight: fontSizeToLineHeight(text.fontSize, lineHeight) }), title: Object.assign(Object.assign({}, title), { fontSize: remsToPixels(title.fontSize), lineHeight: fontSizeToLineHeight(title.fontSize, lineHeight) }) });
|
|
184
195
|
};
|
|
196
|
+
const makeTags = (tags) => {
|
|
197
|
+
return Object.assign(Object.assign({}, tags), { image: Object.assign(Object.assign({}, tags.image), { padding: remsToPixels(tags.container.padding), paddingMobile: remsToPixels(tags.container.paddingMobile), margin: remsToPixels(tags.container.margin), marginMobile: remsToPixels(tags.container.marginMobile) }), container: Object.assign(Object.assign({}, tags.container), { padding: remsToPixels(tags.container.padding), paddingMobile: remsToPixels(tags.container.paddingMobile), margin: remsToPixels(tags.container.margin), marginMobile: remsToPixels(tags.container.marginMobile) }), box: Object.assign(Object.assign({}, tags.box), { padding: remsToPixels(tags.box.padding), paddingMobile: remsToPixels(tags.box.paddingMobile), margin: remsToPixels(tags.box.margin), marginMobile: remsToPixels(tags.box.marginMobile) }), text: Object.assign(Object.assign({}, tags.text), { fontSize: remsToPixels(tags.text.fontSize), fontSizeMobile: remsToPixels(tags.text.fontSizeMobile) }) });
|
|
198
|
+
};
|
|
185
199
|
const makeLogo = (logo) => {
|
|
186
200
|
return {
|
|
187
201
|
marginTop: remsToPixels(logo.marginTop),
|
|
@@ -206,11 +220,13 @@ export const decorateTheme = (theme) => {
|
|
|
206
220
|
const categories = makeCategories(theme.categories);
|
|
207
221
|
const menuItems = makeMenuItems(theme.menuItems, theme.lineHeight);
|
|
208
222
|
const modifiers = makeModifiers(theme.modifiers, theme.lineHeight);
|
|
223
|
+
const tags = makeTags(theme.tags);
|
|
209
224
|
return Object.assign(Object.assign({}, theme), { logo,
|
|
210
225
|
logoFooter, fonts: fonts, lineHeight, layout: layout, buttons: buttons, border, inputs: inputs, counts,
|
|
211
226
|
cards, welcome: welcome, item: item, categories,
|
|
212
227
|
menuItems,
|
|
213
|
-
modifiers
|
|
228
|
+
modifiers,
|
|
229
|
+
tags });
|
|
214
230
|
};
|
|
215
231
|
export const hexToRgbA = (hex, opacity) => {
|
|
216
232
|
let c;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-tender/utils",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
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,11 @@
|
|
|
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
|
+
"@open-tender/types": "^0.3.10",
|
|
39
40
|
"@reduxjs/toolkit": "^1.8.5",
|
|
40
41
|
"@types/google.maps": "^3.53.5",
|
|
42
|
+
"@types/jest": "^29.5.11",
|
|
41
43
|
"@types/react": "^18.0.18",
|
|
42
44
|
"@types/react-dom": "^18.0.6",
|
|
43
45
|
"@types/uuid": "^8.3.4",
|
|
@@ -48,9 +50,11 @@
|
|
|
48
50
|
"eslint-plugin-prettier": "^4.2.1",
|
|
49
51
|
"eslint-plugin-react": "^7.31.7",
|
|
50
52
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
53
|
+
"jest": "^29.7.0",
|
|
51
54
|
"prettier": "^2.7.1",
|
|
52
55
|
"react": "^18.2.0",
|
|
53
56
|
"react-redux": "^8.0.2",
|
|
57
|
+
"ts-jest": "^29.1.1",
|
|
54
58
|
"typescript": "^4.8.2"
|
|
55
59
|
},
|
|
56
60
|
"peerDependencies": {
|
package/dist/.DS_Store
DELETED
|
Binary file
|