@open-tender/utils 0.3.5 → 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.
@@ -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
+ });
@@ -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
- if (!rems)
12
+ const cleanedUpRems = rems === null || rems === void 0 ? void 0 : rems.trim().replace(/\s+/g, ' ');
13
+ if (!cleanedUpRems)
12
14
  return '0';
13
- const pixels = parseFloat(rems.replace('rem', '')) * 10;
14
- const adjustedPixels = responsive ? pixels : pixels;
15
- const noZeroes = parseFloat(adjustedPixels.toFixed(2)).toString();
16
- return noZeroes === '0' ? '0' : `${noZeroes}px`;
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(' ')
@@ -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,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
+ });
@@ -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
- if (!rems)
8
+ const cleanedUpRems = rems === null || rems === void 0 ? void 0 : rems.trim().replace(/\s+/g, ' ');
9
+ if (!cleanedUpRems)
8
10
  return '0';
9
- const pixels = parseFloat(rems.replace('rem', '')) * 10;
10
- const adjustedPixels = responsive ? pixels : pixels;
11
- const noZeroes = parseFloat(adjustedPixels.toFixed(2)).toString();
12
- return noZeroes === '0' ? '0' : `${noZeroes}px`;
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(' ')
@@ -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.5",
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",
@@ -36,6 +36,7 @@
36
36
  "private": false,
37
37
  "devDependencies": {
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",
41
42
  "@types/jest": "^29.5.11",