| 1 |
/** |
| 2 |
* Internal deendencies. |
| 3 |
*/ |
| 4 |
import { currencyCode, facets, locale } from './config'; |
| 5 |
import { sanitizeArg, sanitizeParam } from './utilities'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Clear facet filters from a set of args. |
| 9 |
* |
| 10 |
* @param {object} args Args to clear facets from. |
| 11 |
* @returns {object} Cleared args. |
| 12 |
*/ |
| 13 |
export const clearFacetsFromArgs = (args) => { |
| 14 |
const clearedArgs = { ...args }; |
| 15 |
|
| 16 |
facets.forEach(({ name, type }) => { |
| 17 |
switch (type) { |
| 18 |
case 'price_range': |
| 19 |
delete clearedArgs.max_price; |
| 20 |
delete clearedArgs.min_price; |
| 21 |
break; |
| 22 |
default: |
| 23 |
delete clearedArgs[name]; |
| 24 |
break; |
| 25 |
} |
| 26 |
}); |
| 27 |
|
| 28 |
return clearedArgs; |
| 29 |
}; |
| 30 |
|
| 31 |
/** |
| 32 |
* Format a date. |
| 33 |
* |
| 34 |
* @param {string} date Date string. |
| 35 |
* @returns {string} Formatted number. |
| 36 |
*/ |
| 37 |
export const formatDate = (date) => { |
| 38 |
return new Date(date).toLocaleString(locale, { dateStyle: 'long' }); |
| 39 |
}; |
| 40 |
|
| 41 |
/** |
| 42 |
* Format a number as a price. |
| 43 |
* |
| 44 |
* @param {number} number Number to format. |
| 45 |
* @param {object} options Formatter options. |
| 46 |
* @returns {string} Formatted number. |
| 47 |
*/ |
| 48 |
export const formatPrice = (number, options) => { |
| 49 |
const format = new Intl.NumberFormat(navigator.language, { |
| 50 |
style: 'currency', |
| 51 |
currency: currencyCode, |
| 52 |
currencyDisplay: 'narrowSymbol', |
| 53 |
...options, |
| 54 |
}); |
| 55 |
|
| 56 |
return format.format(number); |
| 57 |
}; |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the post types from a search form. |
| 61 |
* |
| 62 |
* @param {HTMLFormElement} form Form element. |
| 63 |
* @returns {Array} Post types. |
| 64 |
*/ |
| 65 |
export const getPostTypesFromForm = (form) => { |
| 66 |
const data = new FormData(form); |
| 67 |
|
| 68 |
if (data.has('post_type')) { |
| 69 |
return data.getAll('post_type').slice(-1); |
| 70 |
} |
| 71 |
|
| 72 |
if (data.has('post_type[]')) { |
| 73 |
return data.getAll('post_type[]'); |
| 74 |
} |
| 75 |
|
| 76 |
return []; |
| 77 |
}; |
| 78 |
|
| 79 |
/** |
| 80 |
* Get permalink URL parameters from args. |
| 81 |
* |
| 82 |
* @typedef {object} ArgSchema |
| 83 |
* @property {string} type Arg type. |
| 84 |
* @property {any} [default] Default arg value. |
| 85 |
* @property {Array} [allowedValues] Array of allowed values. |
| 86 |
* |
| 87 |
* @param {object} args Args |
| 88 |
* @param {ArgSchema} schema Args schema. |
| 89 |
* @param {string} [prefix] Prefix to prepend to args. |
| 90 |
* @returns {URLSearchParams} URLSearchParams instance. |
| 91 |
*/ |
| 92 |
export const getUrlParamsFromArgs = (args, schema, prefix = '') => { |
| 93 |
const urlParams = new URLSearchParams(); |
| 94 |
|
| 95 |
Object.entries(schema).forEach(([arg, options]) => { |
| 96 |
const param = prefix + arg; |
| 97 |
const value = typeof args[arg] !== 'undefined' ? sanitizeParam(args[arg], options) : null; |
| 98 |
|
| 99 |
if (value !== null) { |
| 100 |
urlParams.set(param, value); |
| 101 |
} |
| 102 |
}); |
| 103 |
|
| 104 |
return urlParams; |
| 105 |
}; |
| 106 |
|
| 107 |
/** |
| 108 |
* Build request args from URL parameters using a given schema. |
| 109 |
* |
| 110 |
* @typedef {object} ArgSchema |
| 111 |
* @property {string} type Arg type. |
| 112 |
* @property {any} [default] Default arg value. |
| 113 |
* @property {Array} [allowedValues] Array of allowed values. |
| 114 |
* |
| 115 |
* @param {URLSearchParams} urlParams URL parameters. |
| 116 |
* @param {object.<string, ArgSchema>} schema Schema to build args from. |
| 117 |
* @param {string} [prefix] Parameter prefix. |
| 118 |
* @param {boolean} [useDefaults] Whether to populate params with default values. |
| 119 |
* @returns {object.<string, any>} Query args. |
| 120 |
*/ |
| 121 |
export const getArgsFromUrlParams = (urlParams, schema, prefix = '', useDefaults = true) => { |
| 122 |
const args = Object.entries(schema).reduce((args, [arg, options]) => { |
| 123 |
const param = urlParams.get(prefix + arg); |
| 124 |
const value = |
| 125 |
typeof param !== 'undefined' ? sanitizeArg(param, options, useDefaults) : null; |
| 126 |
|
| 127 |
if (value !== null) { |
| 128 |
args[arg] = value; |
| 129 |
} |
| 130 |
|
| 131 |
return args; |
| 132 |
}, {}); |
| 133 |
|
| 134 |
return args; |
| 135 |
}; |
| 136 |
|