@mframework/layer-commerce 0.0.3
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/README.md +101 -0
- package/app/cart/useCart.ts +1 -0
- package/app/components/ChevronIcon/ChevronIcon.config.ts +17 -0
- package/app/components/DateSelect/DateSelect.config.ts +15 -0
- package/app/components/Field/Field.config.ts +14 -0
- package/app/components/FieldDate/FieldDate.config.ts +16 -0
- package/app/components/Form/Form.type.ts +8 -0
- package/app/components/Product/Product.config.ts +19 -0
- package/app/components/Product/Stock.config.ts +15 -0
- package/app/components/ProductCustomizableOption/ProductCustomizableOption.config.ts +5 -0
- package/app/components/ProductGallery/ProductGallery.config.ts +15 -0
- package/app/components/ProductReviews/ProductReviews.config.ts +8 -0
- package/app/composables/_types.ts +18 -0
- package/app/composables/adapters/abstract/cartAdapter.ts +0 -0
- package/app/composables/adapters/abstract/categoryAdapter.ts +0 -0
- package/app/composables/adapters/abstract/customerAdapter.ts +0 -0
- package/app/composables/adapters/abstract/inventoryAdapter.ts +0 -0
- package/app/composables/adapters/abstract/orderAdapter.ts +0 -0
- package/app/composables/adapters/abstract/productAdapter.ts +7 -0
- package/app/composables/cart/registry.ts +20 -0
- package/app/composables/cart/types.ts +18 -0
- package/app/composables/cart/useCart.ts +15 -0
- package/app/composables/config.ts +19 -0
- package/app/composables/defs/apiDefinitions.ts +55 -0
- package/app/composables/defs/extension.feature +40 -0
- package/app/composables/defs/extension.mocks.ts +39 -0
- package/app/composables/defs/extension.test.ts +280 -0
- package/app/composables/defs/extension.ts +236 -0
- package/app/composables/defs/index.ts +3 -0
- package/app/composables/defs/typeHelpers.ts +8 -0
- package/app/composables/defs/types.ts +136 -0
- package/app/composables/domain/product.ts +12 -0
- package/app/composables/featured-products.ts +20 -0
- package/app/composables/helpers/contextualizedNormalizers.feature +14 -0
- package/app/composables/helpers/contextualizedNormalizers.test.ts +85 -0
- package/app/composables/helpers/contextualizedNormalizers.ts +20 -0
- package/app/composables/helpers/index.ts +1 -0
- package/app/composables/index.ts +9 -0
- package/app/composables/methods/auth.ts +83 -0
- package/app/composables/methods/cart.ts +119 -0
- package/app/composables/methods/category.ts +27 -0
- package/app/composables/methods/checkout.ts +54 -0
- package/app/composables/methods/customer.ts +52 -0
- package/app/composables/methods/helpers.ts +5 -0
- package/app/composables/methods/index.ts +75 -0
- package/app/composables/methods/order.ts +39 -0
- package/app/composables/methods/product.ts +95 -0
- package/app/composables/methods/settings.ts +16 -0
- package/app/composables/models/cart.ts +95 -0
- package/app/composables/models/category.ts +13 -0
- package/app/composables/models/checkout.ts +17 -0
- package/app/composables/models/customer.ts +16 -0
- package/app/composables/models/facets.ts +25 -0
- package/app/composables/models/index.ts +94 -0
- package/app/composables/models/order.ts +43 -0
- package/app/composables/models/product.ts +73 -0
- package/app/composables/models/shared.ts +75 -0
- package/app/composables/orders.ts +69 -0
- package/app/composables/products/registry.ts +86 -0
- package/app/composables/products/types.ts +8 -0
- package/app/composables/products/useEvents.ts +26 -0
- package/app/composables/products/useGiftCards.ts +29 -0
- package/app/composables/products/useProducts.ts +12 -0
- package/app/composables/products/useSubscriptions.ts +29 -0
- package/app/composables/products.ts +30 -0
- package/app/composables/registry.ts +24 -0
- package/app/composables/useAuth.ts +43 -0
- package/app/composables/useBreakpoints/index.ts +1 -0
- package/app/composables/useBreakpoints/useBreakpoints.ts +28 -0
- package/app/composables/useCache.ts +38 -0
- package/app/composables/useCart/__tests__/useCart.spec.ts +11 -0
- package/app/composables/useCart/index.ts +1 -0
- package/app/composables/useCart/types.ts +17 -0
- package/app/composables/useCart/useCart.ts +46 -0
- package/app/composables/useCartShippingMethods/__tests__/useCartShippingMethods.spec.ts +11 -0
- package/app/composables/useCartShippingMethods/index.ts +1 -0
- package/app/composables/useCartShippingMethods/types.ts +17 -0
- package/app/composables/useCartShippingMethods/useCartShippingMethods.ts +47 -0
- package/app/composables/useCatalog.ts +26 -0
- package/app/composables/useContent.ts +26 -0
- package/app/composables/useCustomer/__tests__/useCustomer.spec.ts +25 -0
- package/app/composables/useCustomer/index.ts +2 -0
- package/app/composables/useCustomer/types.ts +17 -0
- package/app/composables/useCustomer/useCustomer.ts +40 -0
- package/app/composables/useCustomerAddress/__tests__/useCustomerAddress.spec.ts +11 -0
- package/app/composables/useCustomerAddress/index.ts +2 -0
- package/app/composables/useCustomerAddress/types.ts +17 -0
- package/app/composables/useCustomerAddress/useCustomerAddress.ts +55 -0
- package/app/composables/useCustomerOrder/__tests__/useCustomerOrder.spec.ts +11 -0
- package/app/composables/useCustomerOrder/adress.ts +10 -0
- package/app/composables/useCustomerOrder/index.ts +2 -0
- package/app/composables/useCustomerOrder/product.ts +37 -0
- package/app/composables/useCustomerOrder/types.ts +40 -0
- package/app/composables/useCustomerOrder/useCustomerOrder.ts +63 -0
- package/app/composables/useCustomerOrders/__tests__/useCustomerOrders.spec.ts +11 -0
- package/app/composables/useCustomerOrders/index.ts +2 -0
- package/app/composables/useCustomerOrders/types.ts +20 -0
- package/app/composables/useCustomerOrders/useCustomerOrders.ts +56 -0
- package/app/composables/useCustomerReturns/__tests__/useCustomerReturns.spec.ts +11 -0
- package/app/composables/useCustomerReturns/index.ts +2 -0
- package/app/composables/useCustomerReturns/types.ts +17 -0
- package/app/composables/useCustomerReturns/useCustomerReturns.ts +41 -0
- package/app/composables/useHandleError/index.ts +1 -0
- package/app/composables/useHandleError/types.ts +11 -0
- package/app/composables/useHandleError/useHandleError.ts +27 -0
- package/app/composables/useInventory.ts +29 -0
- package/app/composables/useLoading.ts +21 -0
- package/app/composables/useNotification.ts +21 -0
- package/app/composables/usePageTitle.ts +20 -0
- package/app/composables/useProduct/index.ts +2 -0
- package/app/composables/useProduct/types.ts +17 -0
- package/app/composables/useProduct/useProduct.ts +42 -0
- package/app/composables/useProductAttribute/__tests__/useProduct.mock.ts +31 -0
- package/app/composables/useProductAttribute/__tests__/useProductAttribute.spec.ts +14 -0
- package/app/composables/useProductAttribute/index.ts +1 -0
- package/app/composables/useProductAttribute/useProductAttribute.ts +37 -0
- package/app/composables/useProductRecommended/__tests__/useProductRecommended.spec.ts +12 -0
- package/app/composables/useProductRecommended/index.ts +1 -0
- package/app/composables/useProductRecommended/types.ts +17 -0
- package/app/composables/useProductRecommended/useProductRecommended.ts +43 -0
- package/app/composables/useProductReviews/__tests__/productReviews.mock.ts +20 -0
- package/app/composables/useProductReviews/__tests__/useProductReviews.spec.ts +22 -0
- package/app/composables/useProductReviews/index.ts +2 -0
- package/app/composables/useProductReviews/types.ts +17 -0
- package/app/composables/useProductReviews/useProductReviews.ts +46 -0
- package/app/composables/useProducts/__tests__/useProducts.spec.ts +11 -0
- package/app/composables/useProducts/types.ts +22 -0
- package/app/composables/useProducts/useProducts.ts +41 -0
- package/app/composables/useTax.ts +27 -0
- package/app/composables/validationRules/index.ts +1 -0
- package/app/composables/validationRules/password.feature +67 -0
- package/app/composables/validationRules/password.test.ts +89 -0
- package/app/composables/validationRules/password.ts +25 -0
- package/app/normalizers/Cart.query.ts +729 -0
- package/app/normalizers/Cart.type.ts +285 -0
- package/app/normalizers/Category.query.ts +146 -0
- package/app/normalizers/Category.type.ts +55 -0
- package/app/normalizers/CheckEmail.query.ts +28 -0
- package/app/normalizers/Checkout.query.ts +253 -0
- package/app/normalizers/Checkout.type.ts +77 -0
- package/app/normalizers/CmsBlock.query.ts +57 -0
- package/app/normalizers/CmsBlock.type.ts +25 -0
- package/app/normalizers/CmsPage.query.ts +59 -0
- package/app/normalizers/CmsPage.type.ts +26 -0
- package/app/normalizers/Config.query.ts +224 -0
- package/app/normalizers/Config.type.ts +196 -0
- package/app/normalizers/ContactForm.query.ts +49 -0
- package/app/normalizers/CreditMemo.type.ts +49 -0
- package/app/normalizers/GiftCard.type.ts +24 -0
- package/app/normalizers/Invoice.type.ts +58 -0
- package/app/normalizers/Menu.query.ts +54 -0
- package/app/normalizers/Menu.type.ts +22 -0
- package/app/normalizers/MyAccount.query.ts +268 -0
- package/app/normalizers/MyAccount.type.ts +129 -0
- package/app/normalizers/NewsletterSubscription.query.ts +38 -0
- package/app/normalizers/Order.query.ts +741 -0
- package/app/normalizers/Order.type.ts +268 -0
- package/app/normalizers/Payment.type.ts +28 -0
- package/app/normalizers/ProductAlerts.query.ts +23 -0
- package/app/normalizers/ProductCompare.query.ts +226 -0
- package/app/normalizers/ProductCompare.type.ts +90 -0
- package/app/normalizers/ProductList.query.ts +1620 -0
- package/app/normalizers/ProductList.type.ts +726 -0
- package/app/normalizers/Region.query.ts +58 -0
- package/app/normalizers/Region.type.ts +23 -0
- package/app/normalizers/Return.type.ts +50 -0
- package/app/normalizers/Review.query.ts +81 -0
- package/app/normalizers/Review.type.ts +42 -0
- package/app/normalizers/Slider.query.ts +72 -0
- package/app/normalizers/Slider.type.ts +26 -0
- package/app/normalizers/StoreInPickUp.query.ts +54 -0
- package/app/normalizers/StoreInPickUp.type.ts +59 -0
- package/app/normalizers/Subscription.type.ts +25 -0
- package/app/normalizers/Transaction.type.ts +20 -0
- package/app/normalizers/UrlRewrites.query.ts +55 -0
- package/app/normalizers/UrlRewrites.type.ts +25 -0
- package/app/normalizers/Wishlist.query.ts +202 -0
- package/app/normalizers/Wishlist.type.ts +42 -0
- package/app/pages/brand/[...slug].vue +76 -0
- package/app/pages/brands.vue +67 -0
- package/app/pages/cart.vue +142 -0
- package/app/pages/compare.vue +166 -0
- package/app/pages/departments/[...slug].vue +353 -0
- package/app/pages/departments/category/[...slug].vue +114 -0
- package/app/pages/incentive/[...id].vue +66 -0
- package/app/pages/invoice/[id].vue +309 -0
- package/app/pages/order/[id].vue +327 -0
- package/app/pages/product/[...id].vue +309 -0
- package/app/pages/product/showcases/index.vue +86 -0
- package/app/pages/shipment/[...id].vue +176 -0
- package/app/pages/shop/[...slug].vue +158 -0
- package/app/pages/shops.vue +76 -0
- package/app/pages/subscription/[...id].vue +147 -0
- package/app/pages/transaction/[...id].vue +74 -0
- package/app/routes/CategoryPage/CategoryPage.config.ts +28 -0
- package/app/routes/CategoryPage/CategoryPage.type.ts +8 -0
- package/app/routes/Checkout/Checkout.config.ts +3 -0
- package/app/routes/Checkout/Checkout.type.ts +14 -0
- package/app/routes/MyAccount/MyAccount.config.ts +1 -0
- package/app/routes/SearchPage/SearchPage.config.ts +1 -0
- package/app/routes/UrlRewrites/UrlRewrites.config.ts +5 -0
- package/app/stores/Cart/Cart.type.ts +26 -0
- package/app/stores/MyAccount/MyAccount.action.ts +7 -0
- package/app/stores/Notification/Notification.action.ts +40 -0
- package/app/stores/Notification/Notification.type.ts +16 -0
- package/app/stores/Popup/Popup.action.ts +30 -0
- package/app/stores/Product/Product.dispatcher.ts +53 -0
- package/app/stores/Product/Product.reducer.ts +12 -0
- package/app/stores/ProductList/ProductList.dispatcher.ts +35 -0
- package/app/stores/ProductList/ProductList.reducer.ts +13 -0
- package/app/stores/Store.type.ts +31 -0
- package/app/stores/Wishlist/Wishlist.type.ts +12 -0
- package/app/stores/cart.ts +218 -0
- package/app/stores/cartStore.ts +224 -0
- package/app/stores/checkout.ts +18 -0
- package/app/stores/compare.ts +65 -0
- package/app/stores/currency.js +29 -0
- package/app/stores/digital-products.js +11 -0
- package/app/stores/orders.ts +172 -0
- package/app/stores/product.ts +34 -0
- package/app/stores/productList.ts +35 -0
- package/app/stores/productListInfo.ts +0 -0
- package/app/stores/products.ts +118 -0
- package/app/stores/recentlyViewedProducts.ts +56 -0
- package/app/stores/review.ts +33 -0
- package/app/stores/storeInPickUp.ts +21 -0
- package/app/stores/user.ts +20 -0
- package/app/stores/wishlist.ts +46 -0
- package/app/types/Account.type.ts +141 -0
- package/app/types/Breadcrumbs.type.ts +23 -0
- package/app/types/CMS.type.ts +33 -0
- package/app/types/Category.type.ts +67 -0
- package/app/types/Checkout.type.ts +58 -0
- package/app/types/Common.type.ts +77 -0
- package/app/types/Config.type.ts +47 -0
- package/app/types/Device.type.ts +37 -0
- package/app/types/Direction.type.ts +23 -0
- package/app/types/Downloadable.type.ts +24 -0
- package/app/types/Error.type.ts +22 -0
- package/app/types/Field.type.ts +151 -0
- package/app/types/Global.type.ts +156 -0
- package/app/types/Graphql.type.ts +4497 -0
- package/app/types/Layout.type.ts +18 -0
- package/app/types/Menu.type.ts +39 -0
- package/app/types/MiniCart.type.ts +61 -0
- package/app/types/NotificationList.type.ts +34 -0
- package/app/types/Order.type.ts +174 -0
- package/app/types/Price.type.ts +72 -0
- package/app/types/ProductCompare.type.ts +24 -0
- package/app/types/ProductList.type.ts +351 -0
- package/app/types/Rating.type.ts +42 -0
- package/app/types/Router.type.ts +67 -0
- package/app/types/Slider.type.ts +25 -0
- package/app/types/StockStatus.type.ts +21 -0
- package/app/types/domain/index.ts +94 -0
- package/app/types/index.ts +33 -0
- package/app/types/shims.d.ts +52 -0
- package/app/types/tilework-opus.d.ts +48 -0
- package/app/utils/Address/Address.type.ts +28 -0
- package/app/utils/Address/index.ts +325 -0
- package/app/utils/Auth/IsSignedIn.ts +4 -0
- package/app/utils/Auth/Token.ts +9 -0
- package/app/utils/Base64/Base64.ts +16 -0
- package/app/utils/Base64/index.ts +12 -0
- package/app/utils/Browser/Browser.ts +29 -0
- package/app/utils/Browser/index.ts +12 -0
- package/app/utils/BrowserDatabase/BrowserDatabase.ts +70 -0
- package/app/utils/BrowserDatabase/index.ts +12 -0
- package/app/utils/CSS/CSS.ts +65 -0
- package/app/utils/CSS/index.ts +12 -0
- package/app/utils/Cache/Cache.ts +99 -0
- package/app/utils/Cart/Cart.ts +300 -0
- package/app/utils/Cart/Cart.type.ts +76 -0
- package/app/utils/Cart/Token.ts +59 -0
- package/app/utils/Cart/index.ts +13 -0
- package/app/utils/Category/Filters.ts +39 -0
- package/app/utils/Category/index.ts +12 -0
- package/app/utils/Common/index.ts +32 -0
- package/app/utils/Compare/Compare.ts +89 -0
- package/app/utils/Compare/index.ts +12 -0
- package/app/utils/Currency/Currency.ts +84 -0
- package/app/utils/Currency/index.ts +12 -0
- package/app/utils/DynamicReducer/DynamicReducer.type.ts +14 -0
- package/app/utils/DynamicReducer/Helper.ts +30 -0
- package/app/utils/DynamicReducer/index.tsx +35 -0
- package/app/utils/ElementTransition/ElementTransition.ts +100 -0
- package/app/utils/Form/Extract.ts +316 -0
- package/app/utils/Form/Form.ts +46 -0
- package/app/utils/Form/Form.type.ts +60 -0
- package/app/utils/Form/Transform.ts +33 -0
- package/app/utils/FormPortalCollector/index.ts +45 -0
- package/app/utils/History/History.type.ts +31 -0
- package/app/utils/History/index.ts +17 -0
- package/app/utils/Manipulations/Array.ts +18 -0
- package/app/utils/Manipulations/Date.ts +36 -0
- package/app/utils/Manipulations/index.ts +14 -0
- package/app/utils/Media/Media.ts +27 -0
- package/app/utils/Media/index.ts +13 -0
- package/app/utils/Menu/Menu.ts +116 -0
- package/app/utils/Menu/Menu.type.ts +37 -0
- package/app/utils/Menu/index.ts +12 -0
- package/app/utils/Mobile/index.ts +13 -0
- package/app/utils/Mobile/isMobile.ts +34 -0
- package/app/utils/Orders/Orders.ts +71 -0
- package/app/utils/Orders/Orders.type.ts +19 -0
- package/app/utils/Orders/index.ts +13 -0
- package/app/utils/Polyfill/index.ts +336 -0
- package/app/utils/Preload/CategoryPreload.ts +74 -0
- package/app/utils/Preload/ProductPreload.ts +28 -0
- package/app/utils/Preload/index.ts +75 -0
- package/app/utils/Price/Price.config.ts +189 -0
- package/app/utils/Price/Price.ts +63 -0
- package/app/utils/Price/index.ts +12 -0
- package/app/utils/Product/Extract.ts +652 -0
- package/app/utils/Product/Product.ts +575 -0
- package/app/utils/Product/Product.type.ts +260 -0
- package/app/utils/Product/Transform.ts +382 -0
- package/app/utils/Product/index.ts +12 -0
- package/app/utils/Promise/MakeCancelable.ts +44 -0
- package/app/utils/Promise/Promise.type.ts +15 -0
- package/app/utils/Promise/index.ts +13 -0
- package/app/utils/Query/Field.ts +71 -0
- package/app/utils/Query/Fragment.ts +22 -0
- package/app/utils/Query/PrepareDocument.ts +119 -0
- package/app/utils/Query/Query.type.ts +26 -0
- package/app/utils/Query/index.ts +15 -0
- package/app/utils/Request/Config.ts +21 -0
- package/app/utils/Request/DataContainer.ts +102 -0
- package/app/utils/Request/Debounce.ts +48 -0
- package/app/utils/Request/Error.ts +31 -0
- package/app/utils/Request/Hash.ts +88 -0
- package/app/utils/Request/LowPriorityLoad.ts +32 -0
- package/app/utils/Request/LowPriorityRender.ts +55 -0
- package/app/utils/Request/Mutation.ts +25 -0
- package/app/utils/Request/Query.ts +25 -0
- package/app/utils/Request/QueryDispatcher.ts +135 -0
- package/app/utils/Request/Request.ts +310 -0
- package/app/utils/Store/Store.type.ts +26 -0
- package/app/utils/Store/index.ts +71 -0
- package/app/utils/Url/Url.ts +297 -0
- package/app/utils/Url/index.ts +12 -0
- package/app/utils/Validator/Config.ts +96 -0
- package/app/utils/Validator/Validator.ts +268 -0
- package/app/utils/Validator/Validator.type.ts +47 -0
- package/app/utils/Validator/index.ts +12 -0
- package/app/utils/Wishlist/Wishlist.ts +65 -0
- package/app/utils/Wishlist/index.ts +12 -0
- package/app/utils/client.ts +280 -0
- package/app/utils/index.ts +53 -0
- package/app/utils/normalizer.ts +23 -0
- package/app/utils/normalizers/magento.ts +29 -0
- package/app/utils/normalizers/shopify.ts +29 -0
- package/nuxt.config.ts +11 -0
- package/package.json +43 -0
- package/tsconfig.json +45 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* M Framework - Flexible backend agnostic framework.
|
|
3
|
+
*
|
|
4
|
+
* Copyright © Meeovi, LTD. All rights reserved.
|
|
5
|
+
* See LICENSE for license details.
|
|
6
|
+
*
|
|
7
|
+
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
|
|
8
|
+
* @package mframework/mframework
|
|
9
|
+
* @link https://github.com/meeovi/mframework
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { History, Location } from 'history';
|
|
13
|
+
type Match = any
|
|
14
|
+
|
|
15
|
+
import { decodeString } from '../../utils/Common';
|
|
16
|
+
import { getStoreState } from '../../utils/Store';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Update query params without adding to history
|
|
20
|
+
* @param {String} name
|
|
21
|
+
* @param {String} value
|
|
22
|
+
* @namespace ../../utils/Url/updateQueryParamWithoutHistory
|
|
23
|
+
*/
|
|
24
|
+
export const updateQueryParamWithoutHistory = (
|
|
25
|
+
name: string,
|
|
26
|
+
value: string,
|
|
27
|
+
history: History,
|
|
28
|
+
location: Location,
|
|
29
|
+
): void => {
|
|
30
|
+
const { search, pathname } = location;
|
|
31
|
+
|
|
32
|
+
const params = new URLSearchParams(search);
|
|
33
|
+
|
|
34
|
+
params.set(name, value);
|
|
35
|
+
history.replace(decodeURIComponent(`${ pathname }?${ params }`));
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Remove query param without adding to history
|
|
40
|
+
* @param {String} name
|
|
41
|
+
* @namespace ../../utils/Url/removeQueryParamWithoutHistory
|
|
42
|
+
*/
|
|
43
|
+
export const removeQueryParamWithoutHistory = (name: string, history: History, location: Location): void => {
|
|
44
|
+
const { search, pathname } = location;
|
|
45
|
+
|
|
46
|
+
const params = new URLSearchParams(search);
|
|
47
|
+
|
|
48
|
+
params.delete(name);
|
|
49
|
+
history.replace(decodeURIComponent(`${ pathname }?${ params }`));
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Get query param from url
|
|
54
|
+
* @param {Object} match match object from react-router
|
|
55
|
+
* @param {Object} location location object from react-router
|
|
56
|
+
* @namespace ../../utils/Url/getUrlParam
|
|
57
|
+
*/
|
|
58
|
+
export const getUrlParam = (match: Match, location: Location): string => {
|
|
59
|
+
const baseUrl = match.path.replace(window.storeRegexText, '').replace('/', '');
|
|
60
|
+
const currentUrl = location.pathname.replace(new RegExp(`^${window.storeRegexText}`, 'i'), '');
|
|
61
|
+
|
|
62
|
+
if (baseUrl === '/') {
|
|
63
|
+
return currentUrl.replace(baseUrl, '');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return currentUrl.replace(baseUrl, '').replace(/^\/*/, '');
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** @namespace ../../utils/Url/trimEndSlash */
|
|
70
|
+
export const trimEndSlash = (str: string): string => (str.endsWith('/') ? str.slice(0, -1) : str);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Replaces section of URL with passed path value
|
|
74
|
+
* @param {RegExp} regex replacement rule
|
|
75
|
+
* @param {String} path replacement element
|
|
76
|
+
* @returns {*}
|
|
77
|
+
* @namespace ../../utils/Url/replace
|
|
78
|
+
*/
|
|
79
|
+
export const replace = (regex: RegExp, path: string): string => {
|
|
80
|
+
const { pathname = '' } = new URL(window.location.href);
|
|
81
|
+
|
|
82
|
+
return pathname.replace(regex, path);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Append store code to URL
|
|
87
|
+
* @param {String} pathname the URL to append store code to
|
|
88
|
+
* @namespace ../../utils/Url/appendWithStoreCode
|
|
89
|
+
*/
|
|
90
|
+
export const appendWithStoreCode = (pathname: string): string => {
|
|
91
|
+
const { ConfigReducer: { base_link_url = window.location.href } = {} } = getStoreState();
|
|
92
|
+
const { pathname: storePrefix } = new URL(base_link_url);
|
|
93
|
+
|
|
94
|
+
if (!pathname) {
|
|
95
|
+
return trimEndSlash(storePrefix);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// match URLs which have the store code in pathname
|
|
99
|
+
if (new RegExp(`^/(${window.storeList.join('|')})/`, 'i').test(pathname)) {
|
|
100
|
+
return pathname;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// trim the last slash from URL, and append it to pathname
|
|
104
|
+
return trimEndSlash(storePrefix).concat(!pathname.startsWith('/') ? `/${ pathname }` : pathname);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get query variable value (from react router)
|
|
109
|
+
* @param {String} variable Variable from URL
|
|
110
|
+
* @param {Object} location location object from react-router
|
|
111
|
+
* @param {Boolean} isReplaceEqual condition to replace equal sign
|
|
112
|
+
* @return {String|false} Variable value
|
|
113
|
+
* @namespace ../../utils/Url/getQueryParam
|
|
114
|
+
*/
|
|
115
|
+
export const getQueryParam = (
|
|
116
|
+
variable: string,
|
|
117
|
+
location: Location,
|
|
118
|
+
isReplaceEqual: boolean = true,
|
|
119
|
+
): string | false => {
|
|
120
|
+
const query = decodeString(location.search.substring(1));
|
|
121
|
+
const vars = query.split(/[?&]/);
|
|
122
|
+
|
|
123
|
+
return vars.reduce((acc: string | false, item: string) => {
|
|
124
|
+
const splitIdx = item.indexOf('=');
|
|
125
|
+
const [k, v] = [item.slice(0, splitIdx), item.slice(splitIdx + 1)];
|
|
126
|
+
|
|
127
|
+
if (k !== variable) {
|
|
128
|
+
return acc;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return isReplaceEqual ? v.replace(/=/g, ':') : v;
|
|
132
|
+
}, false);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Convert url params to object with key value pairs
|
|
137
|
+
* @param {String} queryString url query string
|
|
138
|
+
* @return {Object} Key-Value pairs
|
|
139
|
+
* @namespace ../../utils/Url/convertQueryStringToKeyValuePairs
|
|
140
|
+
*/
|
|
141
|
+
export const convertQueryStringToKeyValuePairs = <T extends Record<string, string> = Record<string, string>>(
|
|
142
|
+
queryString: string,
|
|
143
|
+
): T => {
|
|
144
|
+
const keyValuePairs: Record<string, string> = {};
|
|
145
|
+
const params = queryString.substring(1).split('&');
|
|
146
|
+
|
|
147
|
+
params.forEach((param) => {
|
|
148
|
+
const pair = param.split('=');
|
|
149
|
+
const [keyPair, valuePair] = pair;
|
|
150
|
+
|
|
151
|
+
if (keyPair.length > 0 && valuePair.length > 0) {
|
|
152
|
+
keyValuePairs[keyPair] = decodeURIComponent(valuePair);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
return keyValuePairs as T;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Update existing key value pairs and return result
|
|
161
|
+
* @param {Object} keyValuePairs current key value pairs
|
|
162
|
+
* @param {String} currentKey key of the value to be updated
|
|
163
|
+
* @param {String} currentValue value to be updated
|
|
164
|
+
* @return {Object} Key-Value pairs
|
|
165
|
+
* @namespace ../../utils/Url/updateKeyValuePairs
|
|
166
|
+
*/
|
|
167
|
+
export const updateKeyValuePairs = (
|
|
168
|
+
keyValuePairs: Record<string, string | number>,
|
|
169
|
+
currentKey: string,
|
|
170
|
+
currentValue: string | number,
|
|
171
|
+
): Record<string, string | number> => {
|
|
172
|
+
const updatedKeyValuePairs: Record<string, string | number> = {};
|
|
173
|
+
|
|
174
|
+
Object.entries(keyValuePairs).forEach((pair) => {
|
|
175
|
+
const [key, value] = pair;
|
|
176
|
+
|
|
177
|
+
if (currentKey === key) {
|
|
178
|
+
updatedKeyValuePairs[key] = currentValue;
|
|
179
|
+
} else {
|
|
180
|
+
updatedKeyValuePairs[key] = value;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
return updatedKeyValuePairs;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Convert object with key value pairs to url query string
|
|
189
|
+
* @param {Object} keyValuePairs object with key value pairs
|
|
190
|
+
* @return {String} Converted query string
|
|
191
|
+
* @namespace ../../utils/Url/convertKeyValuesToQueryString
|
|
192
|
+
*/
|
|
193
|
+
export const convertKeyValuesToQueryString = (
|
|
194
|
+
keyValuePairs: Record<string, string | number>,
|
|
195
|
+
): string => Object.entries(keyValuePairs)
|
|
196
|
+
.map((pair) => {
|
|
197
|
+
const [key, value] = pair;
|
|
198
|
+
const keyExists = key !== '';
|
|
199
|
+
const valueExists = value !== '';
|
|
200
|
+
|
|
201
|
+
if (valueExists && keyExists) {
|
|
202
|
+
return `${key}=${value}`;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return null;
|
|
206
|
+
})
|
|
207
|
+
.filter((x) => !!x)
|
|
208
|
+
.join('&');
|
|
209
|
+
|
|
210
|
+
/** @namespace ../../utils/Url/generateQuery */
|
|
211
|
+
export const generateQuery = (
|
|
212
|
+
keyValueObject: Record<string, string | number>,
|
|
213
|
+
location: Location,
|
|
214
|
+
history: History,
|
|
215
|
+
): string => Object.entries(keyValueObject)
|
|
216
|
+
.reduce((acc, pair) => {
|
|
217
|
+
const [key, value] = pair;
|
|
218
|
+
|
|
219
|
+
const keyAndValueExist = !!key && !!value;
|
|
220
|
+
|
|
221
|
+
if (acc === '' && keyAndValueExist) {
|
|
222
|
+
return `?${key}=${value}`;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (getQueryParam(key, location) !== false) {
|
|
226
|
+
const keyValuePairs = convertQueryStringToKeyValuePairs(acc);
|
|
227
|
+
const updatedKeyValuePairs = updateKeyValuePairs(keyValuePairs, key, value);
|
|
228
|
+
const updatedQuery = convertKeyValuesToQueryString(updatedKeyValuePairs);
|
|
229
|
+
|
|
230
|
+
return updatedQuery.length ? `?${updatedQuery}` : '';
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (keyAndValueExist) {
|
|
234
|
+
return `${acc}&${key}=${value}`;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return acc;
|
|
238
|
+
}, history.location.search);
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Set add key value pairs to url
|
|
242
|
+
* @param {Object} variable Object with key value pairs to be added to url
|
|
243
|
+
* @param {Object} variable location object from react-router
|
|
244
|
+
* @param {Object} variable react router history object
|
|
245
|
+
* @param {Object} variable is url flush required
|
|
246
|
+
* @namespace ../../utils/Url/setQueryParams
|
|
247
|
+
*/
|
|
248
|
+
export const setQueryParams = (keyValueObject: Record<string, string>, location: Location, history: History): void => {
|
|
249
|
+
const { state } = location;
|
|
250
|
+
const query = generateQuery(keyValueObject, location, history);
|
|
251
|
+
|
|
252
|
+
history.push({ search: query } as any);
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Remove all queries except default sort options from url
|
|
257
|
+
* @param {Object} variable react router history object
|
|
258
|
+
* @namespace ../../utils/Url/clearQueriesFromUrl
|
|
259
|
+
*/
|
|
260
|
+
export const clearQueriesFromUrl = (history: History): void => {
|
|
261
|
+
history.push({ search: '' });
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Convert object with key value pairs to url query string
|
|
266
|
+
* @param {Object} keyValuePairs object with key value pairs
|
|
267
|
+
* @return {String} Converted query string
|
|
268
|
+
* @namespace ../../utils/Url/objectToUri
|
|
269
|
+
*/
|
|
270
|
+
export const objectToUri = (keyValueObject: Record<string, string> = {}): string => {
|
|
271
|
+
const paramString = Object.entries(keyValueObject).sort()
|
|
272
|
+
.reduce((acc, [key, value]) => `${acc}&${key}=${value}`, '')
|
|
273
|
+
.replace('&', '');
|
|
274
|
+
|
|
275
|
+
return paramString.length > 0 ? `?${paramString}` : '';
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
/** @namespace ../../utils/Url/isHomePageUrl */
|
|
279
|
+
export const isHomePageUrl = (pathname: string): boolean => {
|
|
280
|
+
const isHomePage = pathname === appendWithStoreCode('/')
|
|
281
|
+
|| pathname === '/'
|
|
282
|
+
|| pathname === appendWithStoreCode('')
|
|
283
|
+
|| pathname === '';
|
|
284
|
+
|
|
285
|
+
return isHomePage;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/** @namespace ../../utils/Url/getUrlPathname */
|
|
289
|
+
export const getUrlPathname = (url: string): string => {
|
|
290
|
+
try {
|
|
291
|
+
const { pathname } = new URL(url);
|
|
292
|
+
|
|
293
|
+
return pathname;
|
|
294
|
+
} catch (e) {
|
|
295
|
+
return url;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* M Framework - Flexible backend agnostic framework.
|
|
3
|
+
*
|
|
4
|
+
* Copyright © Meeovi, LTD. All rights reserved.
|
|
5
|
+
* See LICENSE for license details.
|
|
6
|
+
*
|
|
7
|
+
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
|
|
8
|
+
* @package mframework/mframework
|
|
9
|
+
* @link https://github.com/meeovi/mframework
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export * from './Url';
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
/* eslint-disable spaced-comment */
|
|
3
|
+
/**
|
|
4
|
+
* M Framework - Flexible backend agnostic framework.
|
|
5
|
+
*
|
|
6
|
+
* Copyright © Meeovi, LTD. All rights reserved.
|
|
7
|
+
* See LICENSE for license details.
|
|
8
|
+
*
|
|
9
|
+
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
|
|
10
|
+
* @package mframework/mframework-theme
|
|
11
|
+
* @link https://github.com/meeovi/mframework-theme
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export enum ValidationInputTypeText {
|
|
15
|
+
ALPHA = 'alpha',
|
|
16
|
+
ALPHASPACE = 'alphaSpace',
|
|
17
|
+
ALPHANUMERIC = 'alphaNumeric',
|
|
18
|
+
ALPHADASH = 'alphaDash',
|
|
19
|
+
URL = 'url',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export enum ValidationInputTypeNumber {
|
|
23
|
+
NUMERIC = 'numeric',
|
|
24
|
+
NUMERICDASH = 'numericDash',
|
|
25
|
+
INTEGER = 'integer',
|
|
26
|
+
DECIMAL = 'decimal',
|
|
27
|
+
NATURAL = 'natural',
|
|
28
|
+
NATURALNOZERO = 'naturalNoZero',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export enum ValidationInputTypeInput {
|
|
32
|
+
EMAIL = 'email',
|
|
33
|
+
PASSWORD = 'password',
|
|
34
|
+
DATE = 'date',
|
|
35
|
+
PHONE = 'phone',
|
|
36
|
+
EMAILLIST = 'emailList',
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const ValidationInputType = {
|
|
40
|
+
...ValidationInputTypeInput,
|
|
41
|
+
...ValidationInputTypeNumber,
|
|
42
|
+
...ValidationInputTypeText,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const VALIDATION_MESSAGES = {
|
|
46
|
+
//#region VALIDATION RULE MSG
|
|
47
|
+
isRequired: __('This field is required!'),
|
|
48
|
+
match: __('Incorrect input!'),
|
|
49
|
+
range: __('Value is out of range!'), // Range values are also in Validator.js as they require args
|
|
50
|
+
group: __('Field contains issues!'),
|
|
51
|
+
fileExtension: __('Incorrect File extension upload!'),
|
|
52
|
+
//#endregion
|
|
53
|
+
|
|
54
|
+
//#region VALIDATION RULE MSG
|
|
55
|
+
[ValidationInputType.ALPHA]: __('Incorrect input! Only letters allowed!'),
|
|
56
|
+
[ValidationInputType.ALPHASPACE]: __('Incorrect input! Only words allowed!'),
|
|
57
|
+
[ValidationInputType.ALPHANUMERIC]: __('Incorrect input! Alpha-Numeric value required!'),
|
|
58
|
+
[ValidationInputType.ALPHADASH]: __('Incorrect input! Alpha-Dash value required!'),
|
|
59
|
+
[ValidationInputType.URL]: __('Incorrect input! URL required!'),
|
|
60
|
+
[ValidationInputType.NUMERIC]: __('Incorrect input! Numeric value required!'),
|
|
61
|
+
[ValidationInputType.NUMERICDASH]: __('Incorrect input! Numeric-Dash value required!'),
|
|
62
|
+
[ValidationInputType.INTEGER]: __('Incorrect input! Integer required!'),
|
|
63
|
+
[ValidationInputType.NATURAL]: __('Incorrect input! Natural number required!'),
|
|
64
|
+
[ValidationInputType.NATURALNOZERO]: __('Incorrect input!'),
|
|
65
|
+
[ValidationInputType.EMAIL]: __('Incorrect email format!'),
|
|
66
|
+
[ValidationInputType.EMAILLIST]: __('Incorrect list of emails!'),
|
|
67
|
+
[ValidationInputType.DATE]: __('Incorrect date input!'),
|
|
68
|
+
[ValidationInputType.PASSWORD]: __('Incorrect password input!'),
|
|
69
|
+
[ValidationInputType.PHONE]: __('Incorrect phone input!'),
|
|
70
|
+
//#endregion
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const VALIDATION_RULES = {
|
|
74
|
+
// Text
|
|
75
|
+
[ValidationInputType.ALPHA]: /^[a-z]+$/i,
|
|
76
|
+
[ValidationInputType.ALPHASPACE]: /^[a-z\s]+$/i,
|
|
77
|
+
[ValidationInputType.ALPHANUMERIC]: /^[a-z0-9]+$/i,
|
|
78
|
+
[ValidationInputType.ALPHADASH]: /^[a-z0-9_\\-]+$/i,
|
|
79
|
+
[ValidationInputType.URL]: /^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\\/]))?$/,
|
|
80
|
+
|
|
81
|
+
// Numbers
|
|
82
|
+
[ValidationInputType.NUMERIC]: /^[0-9]+$/,
|
|
83
|
+
[ValidationInputType.NUMERICDASH]: /^[\d\-\s]+$/,
|
|
84
|
+
[ValidationInputType.INTEGER]: /^\\-?[0-9]+$/,
|
|
85
|
+
[ValidationInputType.DECIMAL]: /^\\-?[0-9]*\.?[0-9]+$/,
|
|
86
|
+
[ValidationInputType.NATURAL]: /^[0-9]+$/i,
|
|
87
|
+
[ValidationInputType.NATURALNOZERO]: /^[1-9][0-9]*$/i,
|
|
88
|
+
|
|
89
|
+
// Inputs
|
|
90
|
+
// eslint-disable-next-line max-len
|
|
91
|
+
[ValidationInputType.EMAIL]: /^(([^<>()\\[\]\\.,;:\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-Z0-9]{2,}))$/,
|
|
92
|
+
[ValidationInputType.DATE]: /\d{4}-\d{1,2}-\d{1,2}/,
|
|
93
|
+
[ValidationInputType.PHONE]: /^[\\+]?[(]?[0-9]{3}[)]?[-\s\\.]?[0-9]{3}[-\s\\.]?[0-9]{4,6}$/im,
|
|
94
|
+
// eslint-disable-next-line max-len
|
|
95
|
+
[ValidationInputType.EMAILLIST]: /^(([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\.([a-zA-Z]{2,5}){1,25})+([,\s]+(([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/,
|
|
96
|
+
};
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/* eslint-disable spaced-comment */
|
|
2
|
+
/**
|
|
3
|
+
* M Framework - Flexible backend agnostic framework.
|
|
4
|
+
*
|
|
5
|
+
* Copyright © Meeovi, LTD. All rights reserved.
|
|
6
|
+
* See LICENSE for license details.
|
|
7
|
+
*
|
|
8
|
+
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
|
|
9
|
+
* @package mframework/mframework
|
|
10
|
+
* @link https://github.com/meeovi/mframework
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { FieldType } from 'Component/Field/Field.config';
|
|
14
|
+
import {
|
|
15
|
+
VALIDATION_MESSAGES,
|
|
16
|
+
VALIDATION_RULES,
|
|
17
|
+
ValidationInputTypeNumber,
|
|
18
|
+
} from '../../utils/Validator/Config';
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
ValidationDOMOutput,
|
|
22
|
+
ValidationOutput,
|
|
23
|
+
ValidationRule,
|
|
24
|
+
} from './Validator.type';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Validates parameter based on rules
|
|
28
|
+
* @param value
|
|
29
|
+
* @param rule
|
|
30
|
+
* @returns {boolean|{errorMessages: *[], value}}
|
|
31
|
+
* @namespace ../../utils/Validator/validate
|
|
32
|
+
*/
|
|
33
|
+
export const validate = (value: string | boolean, rule: ValidationRule): boolean | ValidationOutput => {
|
|
34
|
+
const {
|
|
35
|
+
isRequired,
|
|
36
|
+
inputType,
|
|
37
|
+
match,
|
|
38
|
+
range,
|
|
39
|
+
fileExtension,
|
|
40
|
+
customErrorMessages: {
|
|
41
|
+
onRequirementFail,
|
|
42
|
+
onInputTypeFail,
|
|
43
|
+
onMatchFail,
|
|
44
|
+
onRangeFailMin,
|
|
45
|
+
onRangeFailMax,
|
|
46
|
+
onExtensionFail,
|
|
47
|
+
} = {},
|
|
48
|
+
} = rule;
|
|
49
|
+
|
|
50
|
+
const output: ValidationOutput = {
|
|
51
|
+
value,
|
|
52
|
+
errorMessages: [],
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
//#region IS REQUIRED
|
|
56
|
+
if (isRequired && !value) {
|
|
57
|
+
output.errorMessages.push(onRequirementFail || VALIDATION_MESSAGES.isRequired);
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
|
|
61
|
+
//#region INPUT TYPE
|
|
62
|
+
if (inputType && value && !(value as string).match(
|
|
63
|
+
VALIDATION_RULES[inputType as keyof typeof VALIDATION_RULES],
|
|
64
|
+
)) {
|
|
65
|
+
output.errorMessages.push(
|
|
66
|
+
onInputTypeFail || VALIDATION_MESSAGES[inputType as keyof typeof VALIDATION_MESSAGES],
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
|
|
71
|
+
//#region MATCH
|
|
72
|
+
if (typeof match === 'function') {
|
|
73
|
+
const response = match(value as string);
|
|
74
|
+
|
|
75
|
+
if (response !== true) {
|
|
76
|
+
output.errorMessages.push(response === false ? onMatchFail || VALIDATION_MESSAGES.match : response);
|
|
77
|
+
}
|
|
78
|
+
} else if (match && !(value as string).match(match)) {
|
|
79
|
+
output.errorMessages.push(onMatchFail || VALIDATION_MESSAGES.match);
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
|
|
83
|
+
//#region RANGE
|
|
84
|
+
if (range) {
|
|
85
|
+
const { min, max, showLengthError } = range;
|
|
86
|
+
const isNumber = !!(inputType && (<any>Object).values(ValidationInputTypeNumber).includes(inputType));
|
|
87
|
+
|
|
88
|
+
if (isNumber) {
|
|
89
|
+
if (min && +value < min) {
|
|
90
|
+
output.errorMessages.push(onRangeFailMin || __('Minimal value is %s!', min));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (max && +value > max) {
|
|
94
|
+
output.errorMessages.push(onRangeFailMax || __('Maximum value is %s!', max));
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
if (min && (value as string).length < min && (value as string).length > 0) {
|
|
98
|
+
output.errorMessages.push(onRangeFailMin || __('Minimum %s characters!', min));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (max && (value as string).length > max) {
|
|
102
|
+
const tooMany = (value as string).length - max;
|
|
103
|
+
|
|
104
|
+
output.errorMessages.push(onRangeFailMax || __('Maximum %s characters (%s too many)', max, tooMany));
|
|
105
|
+
|
|
106
|
+
if (showLengthError) {
|
|
107
|
+
output.errorMessages.unshift(__('Please enter no more than %s characters.', max));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (fileExtension && value !== '') {
|
|
114
|
+
const { accept } = fileExtension;
|
|
115
|
+
const acceptedExtensions = accept.split(', ');
|
|
116
|
+
const currentFileExtension = (value as string).split('.').pop() as string;
|
|
117
|
+
|
|
118
|
+
if (!acceptedExtensions.includes(currentFileExtension)) {
|
|
119
|
+
output.errorMessages.push(onExtensionFail || VALIDATION_MESSAGES.fileExtension);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
|
|
124
|
+
const { errorMessages } = output;
|
|
125
|
+
|
|
126
|
+
return errorMessages.length === 0 ? true : output;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Validates DOM object check itself and children
|
|
131
|
+
* @param DOM
|
|
132
|
+
* @param rule
|
|
133
|
+
* @returns {boolean|{errorMessages: *[], values: *[], errorFields: *[]}}
|
|
134
|
+
* @namespace ../../utils/Validator/validateGroup
|
|
135
|
+
*/
|
|
136
|
+
export const validateGroup = (DOM: HTMLElement, rule?: ValidationRule): true | ValidationDOMOutput => {
|
|
137
|
+
const {
|
|
138
|
+
selector = 'select, input, textarea, .js-validatabale, form, .FieldGroup',
|
|
139
|
+
} = rule || {};
|
|
140
|
+
const fields = DOM.querySelectorAll<HTMLInputElement>(selector);
|
|
141
|
+
|
|
142
|
+
const output: ValidationDOMOutput = {
|
|
143
|
+
values: [],
|
|
144
|
+
errorFields: [],
|
|
145
|
+
errorMessages: [],
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
//#region VALIDATE FIELDS
|
|
149
|
+
fields.forEach((field) => {
|
|
150
|
+
const {
|
|
151
|
+
name,
|
|
152
|
+
value,
|
|
153
|
+
tagName = FieldType.SELECT,
|
|
154
|
+
type = FieldType.SELECT,
|
|
155
|
+
} = field;
|
|
156
|
+
|
|
157
|
+
const fieldType = tagName.toLowerCase() === FieldType.TEXTAREA ? FieldType.TEXTAREA : type;
|
|
158
|
+
const fieldValue = fieldType === FieldType.CHECKBOX || fieldType === FieldType.RADIO ? field.checked : value;
|
|
159
|
+
|
|
160
|
+
output.values?.push({ name, value: fieldValue, type: fieldType });
|
|
161
|
+
|
|
162
|
+
// Invokes validation event for all fields
|
|
163
|
+
const data = { errors: [] };
|
|
164
|
+
const event = new CustomEvent('validate', { detail: data });
|
|
165
|
+
|
|
166
|
+
field.dispatchEvent(event);
|
|
167
|
+
const { errors } = data;
|
|
168
|
+
|
|
169
|
+
if (errors.length > 0) {
|
|
170
|
+
output.errorFields?.push(errors);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
//#endregion
|
|
174
|
+
|
|
175
|
+
//#region VALIDATE GROUP
|
|
176
|
+
if (rule) {
|
|
177
|
+
const {
|
|
178
|
+
isRequired,
|
|
179
|
+
match,
|
|
180
|
+
customErrorMessages: {
|
|
181
|
+
onRequirementFail,
|
|
182
|
+
onMatchFail,
|
|
183
|
+
onGroupFail,
|
|
184
|
+
} = {},
|
|
185
|
+
} = rule;
|
|
186
|
+
|
|
187
|
+
if (isRequired) {
|
|
188
|
+
const containsValue = output.values?.some(({ value, type }) => {
|
|
189
|
+
if (type === FieldType.CHECKBOX || type === FieldType.RADIO) {
|
|
190
|
+
return value;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (typeof value === 'string') {
|
|
194
|
+
return value.length > 0;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return !!value;
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
if (!containsValue) {
|
|
201
|
+
output.errorMessages?.push(onRequirementFail || VALIDATION_MESSAGES.isRequired);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (typeof match === 'function' && !match(output.values)) {
|
|
206
|
+
output.errorMessages?.push(onMatchFail || VALIDATION_MESSAGES.match);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (output.errorMessages?.length === 0 && (output.errorFields?.length || 0) > 0) {
|
|
210
|
+
output.errorMessages?.push(onGroupFail || VALIDATION_MESSAGES.group);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
//#endregion
|
|
214
|
+
const { errorMessages, errorFields } = output;
|
|
215
|
+
|
|
216
|
+
return !errorMessages?.length && !errorFields?.length ? true : output;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Get number of different character classes
|
|
221
|
+
* @param {String} value
|
|
222
|
+
* @return {Number}
|
|
223
|
+
* @namespace ../../utils/Validator/getNumberOfCharacterClasses
|
|
224
|
+
*/
|
|
225
|
+
export const getNumberOfCharacterClasses = (value: string): number => Number(/\d+/.test(value))
|
|
226
|
+
+ Number(/[a-z]+/.test(value))
|
|
227
|
+
+ Number(/[A-Z]+/.test(value))
|
|
228
|
+
+ Number(/[^a-zA-Z0-9]+/.test(value));
|
|
229
|
+
|
|
230
|
+
export default validate;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Validates password
|
|
234
|
+
* @param {String} value
|
|
235
|
+
* @param {{min: {Number|Object}, max: {Number|Object}}} range
|
|
236
|
+
* @param {String} minCharacter
|
|
237
|
+
* @returns {String|Boolean}
|
|
238
|
+
* @namespace ../../utils/Validator/validatePassword
|
|
239
|
+
*/
|
|
240
|
+
export const validatePassword = (
|
|
241
|
+
value: string,
|
|
242
|
+
range: ValidationRule['range'],
|
|
243
|
+
minCharacter: string,
|
|
244
|
+
): string | boolean => {
|
|
245
|
+
if (value.length === 0 || !range) {
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (range.min && value.length < range.min) {
|
|
250
|
+
return __('Minimum %s characters!', range.min);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (range.max && value.length > range.max) {
|
|
254
|
+
return __('Maximum %s characters!', range.max);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const counter = getNumberOfCharacterClasses(value);
|
|
258
|
+
|
|
259
|
+
if (counter < Number(minCharacter)) {
|
|
260
|
+
return __(
|
|
261
|
+
'Minimum of different classes of characters in password is %s. ',
|
|
262
|
+
minCharacter,
|
|
263
|
+
)
|
|
264
|
+
+ __('Classes of characters: Lower Case, Upper Case, Digits, Special Characters.');
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return true;
|
|
268
|
+
};
|