@open-tender/utils 0.0.1

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.
Files changed (67) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +3 -0
  3. package/dist/cjs/index.d.ts +1 -0
  4. package/dist/cjs/index.js +4 -0
  5. package/dist/cjs/utils/cards.d.ts +20 -0
  6. package/dist/cjs/utils/cards.js +153 -0
  7. package/dist/cjs/utils/cart.d.ts +422 -0
  8. package/dist/cjs/utils/cart.js +848 -0
  9. package/dist/cjs/utils/checkout.d.ts +23 -0
  10. package/dist/cjs/utils/checkout.js +108 -0
  11. package/dist/cjs/utils/constants.d.ts +52 -0
  12. package/dist/cjs/utils/constants.js +58 -0
  13. package/dist/cjs/utils/customer.d.ts +3 -0
  14. package/dist/cjs/utils/customer.js +23 -0
  15. package/dist/cjs/utils/datetimes.d.ts +174 -0
  16. package/dist/cjs/utils/datetimes.js +704 -0
  17. package/dist/cjs/utils/errors.d.ts +48 -0
  18. package/dist/cjs/utils/errors.js +143 -0
  19. package/dist/cjs/utils/helpers.d.ts +30 -0
  20. package/dist/cjs/utils/helpers.js +141 -0
  21. package/dist/cjs/utils/index.d.ts +14 -0
  22. package/dist/cjs/utils/index.js +17 -0
  23. package/dist/cjs/utils/keypad.d.ts +9 -0
  24. package/dist/cjs/utils/keypad.js +105 -0
  25. package/dist/cjs/utils/maps.d.ts +264 -0
  26. package/dist/cjs/utils/maps.js +287 -0
  27. package/dist/cjs/utils/orders.d.ts +40 -0
  28. package/dist/cjs/utils/orders.js +191 -0
  29. package/dist/cjs/utils/refunds.d.ts +35 -0
  30. package/dist/cjs/utils/refunds.js +98 -0
  31. package/dist/cjs/utils/rewards.d.ts +18 -0
  32. package/dist/cjs/utils/rewards.js +69 -0
  33. package/dist/cjs/utils/version.d.ts +4 -0
  34. package/dist/cjs/utils/version.js +38 -0
  35. package/dist/esm/index.d.ts +1 -0
  36. package/dist/esm/index.js +1 -0
  37. package/dist/esm/utils/cards.d.ts +20 -0
  38. package/dist/esm/utils/cards.js +144 -0
  39. package/dist/esm/utils/cart.d.ts +422 -0
  40. package/dist/esm/utils/cart.js +805 -0
  41. package/dist/esm/utils/checkout.d.ts +23 -0
  42. package/dist/esm/utils/checkout.js +100 -0
  43. package/dist/esm/utils/constants.d.ts +52 -0
  44. package/dist/esm/utils/constants.js +55 -0
  45. package/dist/esm/utils/customer.d.ts +3 -0
  46. package/dist/esm/utils/customer.js +18 -0
  47. package/dist/esm/utils/datetimes.d.ts +174 -0
  48. package/dist/esm/utils/datetimes.js +631 -0
  49. package/dist/esm/utils/errors.d.ts +48 -0
  50. package/dist/esm/utils/errors.js +135 -0
  51. package/dist/esm/utils/helpers.d.ts +30 -0
  52. package/dist/esm/utils/helpers.js +117 -0
  53. package/dist/esm/utils/index.d.ts +14 -0
  54. package/dist/esm/utils/index.js +14 -0
  55. package/dist/esm/utils/keypad.d.ts +9 -0
  56. package/dist/esm/utils/keypad.js +97 -0
  57. package/dist/esm/utils/maps.d.ts +264 -0
  58. package/dist/esm/utils/maps.js +269 -0
  59. package/dist/esm/utils/orders.d.ts +40 -0
  60. package/dist/esm/utils/orders.js +171 -0
  61. package/dist/esm/utils/refunds.d.ts +35 -0
  62. package/dist/esm/utils/refunds.js +90 -0
  63. package/dist/esm/utils/rewards.d.ts +18 -0
  64. package/dist/esm/utils/rewards.js +63 -0
  65. package/dist/esm/utils/version.d.ts +4 -0
  66. package/dist/esm/utils/version.js +34 -0
  67. package/package.json +65 -0
@@ -0,0 +1,48 @@
1
+ import { RequestError } from '@open-tender/types';
2
+ export declare const handleRespError: (err: RequestError) => {
3
+ title: string;
4
+ detail: string | undefined;
5
+ messages: [string, string][];
6
+ errors: {
7
+ [x: string]: any;
8
+ };
9
+ err: RequestError;
10
+ };
11
+ export declare const processError: (msg: string | null) => string;
12
+ export declare const handleFormErrors: (params: Record<string, string | null>) => Record<string, string>;
13
+ export declare const makeFormErrors: (error: RequestError) => Record<string, string>;
14
+ export declare const handleCheckoutErrors: (errors: RequestError, asMessages?: boolean) => {};
15
+ export declare const errMessages: {
16
+ unknown: string;
17
+ forbidden: string;
18
+ expiredToken: string;
19
+ notAllowed: string;
20
+ duplicates: string;
21
+ errorsBelow: string;
22
+ emptyField: string;
23
+ invalidAmount: string;
24
+ invalidEmail: string;
25
+ invalidEmails: string;
26
+ notInteger: string;
27
+ positiveInteger: string;
28
+ invalidTime: string;
29
+ invalidDate: string;
30
+ invalidNumber: string;
31
+ invalidHex: string;
32
+ invalidZip: string;
33
+ invalidPhone: string;
34
+ invalidPassword: string;
35
+ invalidToken: string;
36
+ requiredField: string;
37
+ invalidFilename: string;
38
+ notExist: string;
39
+ serverError: string;
40
+ invalidHours: string;
41
+ revenueCenterClosed: string;
42
+ serviceTypeNotAvailable: string;
43
+ notInZone: string;
44
+ missingAddress: string;
45
+ orderNotFound: string;
46
+ distanceSurcharge: string;
47
+ missingAddressDeliveryZone: string;
48
+ };
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errMessages = exports.handleCheckoutErrors = exports.makeFormErrors = exports.handleFormErrors = exports.processError = exports.handleRespError = void 0;
4
+ const helpers_1 = require("./helpers");
5
+ const handleRespError = (err) => {
6
+ const error = typeof err === 'string' ? { detail: err } : err;
7
+ let { detail } = error || {};
8
+ const { title = 'Something went wrong', params } = error || {};
9
+ const messages = params ? Object.entries(params || {}) : [];
10
+ const errors = !params
11
+ ? {}
12
+ : Object.entries(params || {}).reduce((obj, [key, msg]) => {
13
+ const [, attribute, index, field] = key.split('.');
14
+ if (!index)
15
+ return Object.assign(Object.assign({}, obj), { [attribute]: msg });
16
+ const error = { index: JSON.parse(index)[0], [field]: msg };
17
+ const errors = obj[attribute] ? [...obj[attribute], error] : [error];
18
+ return Object.assign(Object.assign({}, obj), { [attribute]: errors });
19
+ }, {});
20
+ if (detail && detail.includes('parameters') && messages.length) {
21
+ detail = messages[0][1];
22
+ }
23
+ return { title, detail, messages, errors, err };
24
+ };
25
+ exports.handleRespError = handleRespError;
26
+ const processError = (msg) => {
27
+ if (!msg)
28
+ return exports.errMessages.unknown;
29
+ if (msg.includes('is a required property')) {
30
+ return exports.errMessages.requiredField;
31
+ }
32
+ else if (msg.includes("'' is too short")) {
33
+ return exports.errMessages.requiredField;
34
+ }
35
+ else if (msg.includes("not a 'email'")) {
36
+ return exports.errMessages.invalidEmail;
37
+ }
38
+ else if (msg.includes("not a 'phone'")) {
39
+ return exports.errMessages.invalidPhone;
40
+ }
41
+ else if (msg.includes("not a 'optional-phone'")) {
42
+ return exports.errMessages.invalidPhone;
43
+ }
44
+ else if (msg.includes('invalid token')) {
45
+ return exports.errMessages.invalidToken;
46
+ }
47
+ else if (msg.includes("not a 'user-password'")) {
48
+ return exports.errMessages.invalidPassword;
49
+ }
50
+ return msg;
51
+ };
52
+ exports.processError = processError;
53
+ const handleFormErrors = (params) => {
54
+ return Object.entries(params).reduce((obj, error) => {
55
+ const [field, msg] = error;
56
+ const message = (0, exports.processError)(msg);
57
+ return Object.assign(Object.assign({}, obj), { [field.replace('$.', '')]: message });
58
+ }, {});
59
+ };
60
+ exports.handleFormErrors = handleFormErrors;
61
+ const makeFormErrors = (error) => {
62
+ let errors = {};
63
+ if (error === null || error === void 0 ? void 0 : error.params) {
64
+ errors = (0, exports.handleFormErrors)(error.params);
65
+ errors.form = error.detail;
66
+ }
67
+ else {
68
+ errors.form = (error === null || error === void 0 ? void 0 : error.detail) || (error === null || error === void 0 ? void 0 : error.message) || "";
69
+ }
70
+ return errors;
71
+ };
72
+ exports.makeFormErrors = makeFormErrors;
73
+ const handleCheckoutErrors = (errors, asMessages = true) => {
74
+ const { detail, params, message } = errors || {};
75
+ if (!params || (0, helpers_1.isEmpty)(params))
76
+ return { form: detail || message };
77
+ const errObj = Object.entries(params).reduce((obj, error) => {
78
+ const [key, value] = error;
79
+ const err = asMessages ? value.message : value;
80
+ const [, entity, index, field] = key.split('.');
81
+ const existingEntity = obj[entity] || {};
82
+ if (!index) {
83
+ return Object.assign(Object.assign({}, obj), { [entity]: err });
84
+ }
85
+ else if (!field) {
86
+ const newEntity = Object.assign(Object.assign({}, existingEntity), { [index]: err });
87
+ return Object.assign(Object.assign({}, obj), { [entity]: newEntity });
88
+ }
89
+ else {
90
+ if (existingEntity) {
91
+ const existingIndex = existingEntity[index] || {};
92
+ const newIndex = Object.assign(Object.assign({}, existingIndex), { [field]: err });
93
+ const newEntity = Object.assign(Object.assign({}, existingEntity), { [index]: newIndex });
94
+ return Object.assign(Object.assign({}, obj), { [entity]: newEntity });
95
+ }
96
+ else {
97
+ return Object.assign(Object.assign({}, obj), { [entity]: { [index]: { [field]: err } } });
98
+ }
99
+ }
100
+ // const newEntity = obj[entity]
101
+ // ? { ...obj[entity], [index]: err }
102
+ // : index
103
+ // ? { [index]: err }
104
+ // : err
105
+ // return { ...obj, [entity]: newEntity }
106
+ }, { form: 'There are one or more errors below' });
107
+ return errObj;
108
+ };
109
+ exports.handleCheckoutErrors = handleCheckoutErrors;
110
+ exports.errMessages = {
111
+ unknown: 'Unknown error. Please contact Open Tender support.',
112
+ forbidden: 'Your account does not have access to this page. Please contact your brand admin to request access.',
113
+ expiredToken: 'Reset token has expired. Please head back to the login form and try again.',
114
+ notAllowed: "This operation isn't allowed for this entity",
115
+ duplicates: 'One or more fields are already in use. Please choose different values.',
116
+ errorsBelow: 'There are one or more errors below.',
117
+ emptyField: 'This field cannot be empty',
118
+ invalidAmount: 'Invalid amount',
119
+ invalidEmail: 'Invalid email address',
120
+ invalidEmails: 'One or more email addresses are invaiid',
121
+ notInteger: 'Must be an integer',
122
+ positiveInteger: 'Must be a positive integer',
123
+ invalidTime: 'Enter in HH:MM AM|PM format (e.g. 9:40 AM)',
124
+ invalidDate: 'Enter in YYYY-MM-DD format or leave blank',
125
+ invalidNumber: 'Enter as a number with or without decimals',
126
+ invalidHex: 'Enter as a hexidecimal color code',
127
+ invalidZip: 'Enter a valid 5 digit zip code',
128
+ invalidPhone: 'Invalid phone number',
129
+ invalidPassword: 'Invalid password. Passwords must be at least 8 characters and include only letters and numbers and any of these special characters: !@#$%^&_-+=<>.',
130
+ invalidToken: 'Password reset token is invalid or expired. Please resubmit your password reset request via the login form.',
131
+ requiredField: 'This field is required',
132
+ invalidFilename: 'Invalid filename. Please rename the file and try again.',
133
+ notExist: 'The requested entity does not exist.',
134
+ serverError: 'Unknown error. Please contact Open Tender support.',
135
+ invalidHours: 'One or more invalid times submitted. Please see below.',
136
+ revenueCenterClosed: 'This location is closed or otherwise not able to accept orders at the selected time. Please try changing your order time.',
137
+ serviceTypeNotAvailable: 'This location does not offer the selected service type at this time. Please switch to another service type.',
138
+ notInZone: 'Selected address not in the delivery zone for this revenue center',
139
+ missingAddress: 'An address is required for delivery orders. Please add one.',
140
+ orderNotFound: "Sorry, but we couldn't find an order matching that ID. Please try again.",
141
+ distanceSurcharge: "Distance-based surcharges can't be applied to orders with requested times in the past. Please adjust your requested delivery time.",
142
+ missingAddressDeliveryZone: "Please add an address for this revenue center or the revenue center's parent store before setting a delivery zone"
143
+ };
@@ -0,0 +1,30 @@
1
+ import { GuestData } from '@open-tender/types';
2
+ export declare const isString: (str: any) => boolean;
3
+ export declare const isObject: (obj: any) => boolean;
4
+ export declare const isEmpty: (obj?: Record<string | number, unknown> | null) => boolean;
5
+ export declare const isNum: (s: string) => boolean;
6
+ export declare const contains: (arr: Array<any>, values: Array<any>) => boolean;
7
+ export declare const slugify: (text: string) => string;
8
+ export declare const stripTags: (s: string) => string;
9
+ export declare const serialize: (obj: Record<string | number, string | number | boolean>) => string;
10
+ export declare const capitalize: (s: string) => string;
11
+ export declare const cleanPhone: (phone: string) => string;
12
+ export declare const makePhone: (phone: string) => string;
13
+ export declare const validatePhone: (phone: string) => boolean;
14
+ export declare const makeBirthDate: (birthDate: string) => string;
15
+ export declare const slashesToDashes: (birthDate: string) => string | null;
16
+ export declare const dashesToSlashes: (birthDate?: string | null) => string | null;
17
+ export declare const validateEmail: (email: string) => boolean;
18
+ export declare const checkEmpty: (data: Record<string | number, unknown>) => boolean;
19
+ export declare const checkGuestData: (data: GuestData, email: string) => {
20
+ guestData: {
21
+ first_name: string;
22
+ last_name: string;
23
+ phone: string;
24
+ email: string;
25
+ };
26
+ guestIncomplete: boolean;
27
+ };
28
+ export declare const makeRandomNumberString: () => string;
29
+ export declare const getWidth: () => number;
30
+ export declare const getHeight: () => number;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getHeight = exports.getWidth = exports.makeRandomNumberString = 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
+ const isString = (str) => {
5
+ return typeof str === 'string';
6
+ };
7
+ exports.isString = isString;
8
+ const isObject = (obj) => {
9
+ return typeof obj === 'object' && obj !== null;
10
+ };
11
+ exports.isObject = isObject;
12
+ const isEmpty = (obj) => {
13
+ return (!obj || (obj.constructor === Object && Object.entries(obj).length === 0));
14
+ };
15
+ exports.isEmpty = isEmpty;
16
+ const isNum = (s) => /^\d+$/.test(s);
17
+ exports.isNum = isNum;
18
+ const contains = (arr, values) => {
19
+ return values.filter(i => arr.includes(i)).length > 0;
20
+ };
21
+ exports.contains = contains;
22
+ // https://gist.github.com/mathewbyrne/1280286
23
+ const slugify = (text) => {
24
+ return text
25
+ .toString()
26
+ .toLowerCase()
27
+ .replace('_', '-') // replace _ with -
28
+ .replace(/\s+/g, '-') // Replace spaces with -
29
+ .replace(/[^\w-]+/g, '') // Remove all non-word chars
30
+ .replace(/--+/g, '-') // Replace multiple - with single -
31
+ .replace(/^-+/, '') // Trim - from start of text
32
+ .replace(/-+$/, ''); // Trim - from end of text
33
+ };
34
+ exports.slugify = slugify;
35
+ const stripTags = (s) => s.replace(/(<([^>]+)>)/gi, '').replace('&amp;', '&');
36
+ exports.stripTags = stripTags;
37
+ const serialize = (obj) => {
38
+ const str = [];
39
+ for (const p in obj)
40
+ if (Object.prototype.hasOwnProperty.call(obj, p)) {
41
+ str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
42
+ }
43
+ return str.join('&');
44
+ };
45
+ exports.serialize = serialize;
46
+ const capitalize = (s) => {
47
+ if (!s || !s.length)
48
+ return '';
49
+ return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
50
+ };
51
+ exports.capitalize = capitalize;
52
+ const cleanPhone = (phone) => {
53
+ if (!phone)
54
+ return '';
55
+ let p = phone.replace(/\D/g, '');
56
+ if (p.length > 11)
57
+ return phone;
58
+ p = p.length === 11 && p[0] === '1' ? p.slice(1, 11) : p;
59
+ p =
60
+ p.length === 10 ? `${p.slice(0, 3)}-${p.slice(3, 6)}-${p.slice(6, 10)}` : p;
61
+ return p;
62
+ };
63
+ exports.cleanPhone = cleanPhone;
64
+ const makePhone = (phone) => {
65
+ if (!phone)
66
+ return '';
67
+ const p = phone.replace(/\D/g, '');
68
+ if (p.length > 6) {
69
+ return `${p.slice(0, 3)}-${p.slice(3, 6)}-${p.slice(6, 10)}`;
70
+ }
71
+ else if (p.length > 3) {
72
+ return `${p.slice(0, 3)}-${p.slice(3, 6)}`;
73
+ }
74
+ return p;
75
+ };
76
+ exports.makePhone = makePhone;
77
+ const validatePhone = (phone) => {
78
+ var _a;
79
+ if (!phone)
80
+ return false;
81
+ return ((_a = phone.match(/\d/g)) === null || _a === void 0 ? void 0 : _a.length) === 10;
82
+ };
83
+ exports.validatePhone = validatePhone;
84
+ const makeBirthDate = (birthDate) => {
85
+ if (!birthDate)
86
+ return '';
87
+ const b = birthDate.replace(/\D/g, '');
88
+ if (b.length > 4) {
89
+ return `${b.slice(0, 2)}/${b.slice(2, 4)}/${b.slice(4, 8)}`;
90
+ }
91
+ else if (b.length > 2) {
92
+ return `${b.slice(0, 2)}/${b.slice(2, 4)}`;
93
+ }
94
+ return b;
95
+ };
96
+ exports.makeBirthDate = makeBirthDate;
97
+ const slashesToDashes = (birthDate) => {
98
+ if (!birthDate)
99
+ return null;
100
+ const parts = birthDate.split('/');
101
+ return `${parts[2] || ''}-${parts[0] || ''}-${parts[1] || ''}`;
102
+ };
103
+ exports.slashesToDashes = slashesToDashes;
104
+ const dashesToSlashes = (birthDate) => {
105
+ if (!birthDate)
106
+ return null;
107
+ const parts = birthDate.split('-');
108
+ return `${parts[1] || ''}/${parts[2] || ''}/${parts[0] || ''}`;
109
+ };
110
+ exports.dashesToSlashes = dashesToSlashes;
111
+ const validateEmail = (email) => {
112
+ const re =
113
+ // eslint-disable-next-line no-useless-escape
114
+ /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
115
+ return re.test(email);
116
+ };
117
+ exports.validateEmail = validateEmail;
118
+ const checkEmpty = (data) => {
119
+ if ((0, exports.isEmpty)(data))
120
+ return true;
121
+ return Object.values(data).filter(i => !i).length > 0;
122
+ };
123
+ exports.checkEmpty = checkEmpty;
124
+ const checkGuestData = (data, email) => {
125
+ const { first_name, last_name, phone } = data;
126
+ const guestData = { first_name, last_name, phone, email };
127
+ const validPhone = (0, exports.validatePhone)(phone) ? phone : null;
128
+ const guestIncomplete = (0, exports.checkEmpty)(Object.assign(Object.assign({}, guestData), { phone: validPhone }));
129
+ return { guestData, guestIncomplete };
130
+ };
131
+ exports.checkGuestData = checkGuestData;
132
+ const makeRandomNumberString = () => Math.floor(Math.random() * 1000000000).toString();
133
+ exports.makeRandomNumberString = makeRandomNumberString;
134
+ const getWidth = () => {
135
+ return Math.max(document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth, document.documentElement.clientWidth);
136
+ };
137
+ exports.getWidth = getWidth;
138
+ const getHeight = () => {
139
+ return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.documentElement.clientHeight);
140
+ };
141
+ exports.getHeight = getHeight;
@@ -0,0 +1,14 @@
1
+ export * from './cards';
2
+ export * from './cart';
3
+ export * from './checkout';
4
+ export * from './constants';
5
+ export * from './customer';
6
+ export * from './datetimes';
7
+ export * from './errors';
8
+ export * from './helpers';
9
+ export * from './keypad';
10
+ export * from './maps';
11
+ export * from './refunds';
12
+ export * from './rewards';
13
+ export * from './version';
14
+ export * from './orders';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./cards"), exports);
5
+ tslib_1.__exportStar(require("./cart"), exports);
6
+ tslib_1.__exportStar(require("./checkout"), exports);
7
+ tslib_1.__exportStar(require("./constants"), exports);
8
+ tslib_1.__exportStar(require("./customer"), exports);
9
+ tslib_1.__exportStar(require("./datetimes"), exports);
10
+ tslib_1.__exportStar(require("./errors"), exports);
11
+ tslib_1.__exportStar(require("./helpers"), exports);
12
+ tslib_1.__exportStar(require("./keypad"), exports);
13
+ tslib_1.__exportStar(require("./maps"), exports);
14
+ tslib_1.__exportStar(require("./refunds"), exports);
15
+ tslib_1.__exportStar(require("./rewards"), exports);
16
+ tslib_1.__exportStar(require("./version"), exports);
17
+ tslib_1.__exportStar(require("./orders"), exports);
@@ -0,0 +1,9 @@
1
+ export declare const centsToDollars: (cents: number) => string;
2
+ export declare const buttonsKeypad: string[][];
3
+ export declare const buttonsNumpad: string[][];
4
+ export declare const buttonsDollars: string[][];
5
+ export declare const buttonsCheckout: string[][];
6
+ export declare const reduceKeypad: (value: string, key: string) => string;
7
+ export declare const reduceNumpad: (value: string, key: string, clear: boolean) => string;
8
+ export declare const reduceDollars: (value: string, key: string, clear: boolean) => string;
9
+ export declare const reduceCheckout: (value: string, key: string, clear: boolean) => string;
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reduceCheckout = exports.reduceDollars = exports.reduceNumpad = exports.reduceKeypad = exports.buttonsCheckout = exports.buttonsDollars = exports.buttonsNumpad = exports.buttonsKeypad = exports.centsToDollars = void 0;
4
+ const centsToDollars = (cents) => (cents / 100).toFixed(2);
5
+ exports.centsToDollars = centsToDollars;
6
+ exports.buttonsKeypad = [
7
+ '1234567890'.split(''),
8
+ 'qwertyuiop'.split(''),
9
+ 'asdfghjkl'.split(''),
10
+ ['Clr', ...'zxcvbnm'.split(''), 'Del']
11
+ ];
12
+ exports.buttonsNumpad = [
13
+ '123'.split(''),
14
+ '456'.split(''),
15
+ '789'.split(''),
16
+ ['Clr', '0', 'Del']
17
+ ];
18
+ exports.buttonsDollars = [
19
+ '123'.split(''),
20
+ '456'.split(''),
21
+ '789'.split(''),
22
+ ['Clr', '0', '00']
23
+ ];
24
+ exports.buttonsCheckout = [
25
+ ['1', '2', '3', '$5'],
26
+ ['4', '5', '6', '$10'],
27
+ ['7', '8', '9', '$20'],
28
+ ['Clr', '0', '00', 'Next $']
29
+ ];
30
+ const reduceKeypad = (value, key) => {
31
+ switch (key) {
32
+ case 'Clear':
33
+ case 'Clr':
34
+ return '';
35
+ case 'Delete':
36
+ case 'Del':
37
+ return value.slice(0, -1);
38
+ case 'Space':
39
+ return value + ' ';
40
+ default:
41
+ return value + key;
42
+ }
43
+ };
44
+ exports.reduceKeypad = reduceKeypad;
45
+ const reduceNumpad = (value, key, clear) => {
46
+ const current = clear ? '' : value;
47
+ switch (key) {
48
+ case 'Clear':
49
+ case 'Clr':
50
+ return '';
51
+ case 'Delete':
52
+ case 'Del':
53
+ return value.slice(0, -1);
54
+ default:
55
+ return current + key;
56
+ }
57
+ };
58
+ exports.reduceNumpad = reduceNumpad;
59
+ const reduceDollars = (value, key, clear) => {
60
+ const current = clear ? '' : value;
61
+ switch (key) {
62
+ case 'Clear':
63
+ case 'Clr':
64
+ return '0.00';
65
+ default: {
66
+ const val = parseInt(current.replace('.', '') + key);
67
+ return (0, exports.centsToDollars)(val);
68
+ }
69
+ }
70
+ };
71
+ exports.reduceDollars = reduceDollars;
72
+ const reduceCheckout = (value, key, clear) => {
73
+ const current = !key.toString().includes('$') && clear ? '' : value;
74
+ switch (key) {
75
+ case 'Clear':
76
+ case 'Clr':
77
+ return '0.00';
78
+ case 'Next $': {
79
+ const [dollars] = current.split('.');
80
+ const val = (parseInt(dollars) + 1) * 100;
81
+ return (0, exports.centsToDollars)(val);
82
+ }
83
+ case '$5':
84
+ case '$10':
85
+ case '$20': {
86
+ const keyValue = parseInt(key.replace('$', '')) * 100;
87
+ const [dollars, cents] = current.split('.').map(n => parseInt(n));
88
+ let val;
89
+ if (cents > 0 || dollars % 10 > 0) {
90
+ val =
91
+ Math.floor(parseInt(current.replace('.', '')) / keyValue) * keyValue +
92
+ keyValue;
93
+ }
94
+ else {
95
+ val = dollars * 100 + keyValue;
96
+ }
97
+ return (0, exports.centsToDollars)(val);
98
+ }
99
+ default: {
100
+ const val = parseInt(current.replace('.', '') + key);
101
+ return (0, exports.centsToDollars)(val);
102
+ }
103
+ }
104
+ };
105
+ exports.reduceCheckout = reduceCheckout;