File size: 1,625 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { formatCurrency } from '@automattic/number-formatters';
import deterministicStringify from 'fast-json-stable-stringify';
import type { DomainSuggestionQuery, DomainSuggestionSelectorOptions } from './types';

/**
 * Stable transform to an object key for storage and access.
 */
export const stringifyDomainQueryObject: ( q: DomainSuggestionQuery ) => string =
	deterministicStringify;

/**
 * Formats the domain suggestion price according to 'format-currency' package rules
 * We use this for consistency in prices formats across plans and domains
 * @param price the domain suggestion raw price
 * @param currencyCode the currency code to be used when formatting price
 */
export function getFormattedPrice( price: number, currencyCode: string ): string {
	return formatCurrency( price, currencyCode, {
		stripZeros: true,
	} ) as string;
}

/**
 * Normalize domain query
 *
 * It's important to have a consistent, reproduceable representation of a domains query so that the result can be
 * stored and retrieved.
 * @param search       Domain search string
 * @param queryOptions Optional paramaters for the query
 * @returns Normalized query object
 */
export function normalizeDomainSuggestionQuery(
	search: string,
	queryOptions: DomainSuggestionSelectorOptions
): DomainSuggestionQuery {
	return {
		// Defaults
		include_wordpressdotcom: queryOptions.only_wordpressdotcom || false,
		include_dotblogsubdomain: false,
		only_wordpressdotcom: false,
		quantity: 5,
		vendor: 'variation2_front',

		// Merge options
		...queryOptions,

		// Add the search query
		query: search.trim().toLocaleLowerCase(),
	};
}