-
Notifications
You must be signed in to change notification settings - Fork 2k
/
utility.js
95 lines (77 loc) · 2.62 KB
/
utility.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* External dependencies
*/
import { find, startsWith } from 'lodash';
/**
* Internal dependencies
*/
import { domainAvailability } from 'lib/domains/constants';
function moveArrayElement( array, from, to ) {
if ( from !== to && from < array.length && to < array.length ) {
array.splice( to, 0, array.splice( from, 1 )[ 0 ] );
}
}
export function markFeaturedSuggestions(
suggestions,
exactMatchDomain,
strippedDomainBase,
featuredSuggestionsAtTop = false,
avoidTlds = []
) {
function isExactMatchBeforeTld( suggestion ) {
return (
suggestion.domain_name === exactMatchDomain ||
startsWith( suggestion.domain_name, `${ strippedDomainBase }.` )
);
}
function isBestAlternative( suggestion ) {
return ! isExactMatchBeforeTld( suggestion ) && suggestion.isRecommended !== true;
}
const output = [ ...suggestions ];
let outputWithoutTlds = filterOutDomainsWithTlds( output, avoidTlds );
const recommendedSuggestion =
( featuredSuggestionsAtTop ? null : find( outputWithoutTlds, isExactMatchBeforeTld ) ) ||
outputWithoutTlds[ 0 ];
if ( recommendedSuggestion ) {
recommendedSuggestion.isRecommended = true;
moveArrayElement( output, output.indexOf( recommendedSuggestion ), 0 );
outputWithoutTlds = filterOutDomainsWithTlds( output, avoidTlds );
}
const bestAlternativeSuggestion =
( featuredSuggestionsAtTop ? null : find( outputWithoutTlds, isBestAlternative ) ) ||
outputWithoutTlds[ 1 ];
if ( bestAlternativeSuggestion ) {
bestAlternativeSuggestion.isBestAlternative = true;
moveArrayElement( output, output.indexOf( bestAlternativeSuggestion ), 1 );
}
return output;
}
export function filterOutDomainsWithTlds( suggestions, disallowedTlds ) {
return suggestions.filter( ( suggestion ) => {
const tld = suggestion.domain_name.split( '.' ).pop();
return disallowedTlds.indexOf( tld ) === -1;
} );
}
export function isUnknownSuggestion( suggestion ) {
return suggestion.status === domainAvailability.UNKNOWN;
}
export function isMissingVendor( suggestion ) {
return ! ( 'vendor' in suggestion );
}
export function isFreeSuggestion( suggestion ) {
return suggestion.is_free === true;
}
export function getStrippedDomainBase( domain ) {
let strippedDomainBase = domain;
const lastIndexOfDot = strippedDomainBase.lastIndexOf( '.' );
if ( lastIndexOfDot !== -1 ) {
strippedDomainBase = strippedDomainBase.substring( 0, lastIndexOfDot );
}
return strippedDomainBase.replace( /[ .]/g, '' );
}
export function isNumberString( string ) {
return /^[0-9_]+$/.test( string );
}
export function getTldWeightOverrides( designType ) {
return designType && designType === 'blog' ? 'design_type_blog' : null;
}