-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
84 lines (76 loc) · 3.23 KB
/
index.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
import { decodeEntities } from '@wordpress/html-entities';
import { __, sprintf } from '@wordpress/i18n';
/**
* Create an option data object to be used with SelectControl for the given post object.
*
* @param {object} post - Post object.
* @param {string} [prefix=''] - Optional. Prefix to use for the title. Defaults to empty string.
* @returns {object} Option data to use with SelectControl.
*/
export function createOptionFromPost( post, prefix = '' ) {
const { id, title } = post;
return {
label: prefix + decodeEntities( title.rendered || sprintf( __( '#%d (no title)', 'block-editor-components' ), id ) ),
value: id,
};
}
/**
* Create an option data object to be used with SelectControl for the given term object.
*
* @param {object} term - Term object.
* @param {string} [prefix=''] - Optional. Prefix to use for the name. Defaults to empty string.
* @returns {object} Option data to use with SelectControl.
*/
export function createOptionFromTerm( term, prefix = '' ) {
const { id, name } = term;
return {
label: prefix + decodeEntities( name || sprintf( __( '#%d (no name)', 'block-editor-components' ), id ) ),
value: id,
};
}
/**
* Create an options array to be used with SelectControl for the given array of post objects.
*
* @param {object[]} posts - Array with post objects as elements.
* @returns {object[]} Options data to use with SelectControl.
*/
export function createOptionsFromPosts( posts ) {
return posts.map( ( post ) => createOptionFromPost( post ) );
}
/**
* Create an options array to be used with SelectControl for the given array of post objects, visualizing the hierarchy.
*
* @param {object[]} posts - Array with post objects as elements.
* @param {string} [prefix='— '] - Optional. Prefix to use for the label. Defaults to "— ".
* @param {number} [level=0] - Optional. Hierarchy level. Defaults to 0.
* @returns {object[]} Options data to use with SelectControl.
*/
export function createOptionsFromPostsWithHierarchy( posts, prefix= '— ', level = 0 ) {
return posts.map( ( { children = [], ...post } ) => [
createOptionFromPost( post, prefix.repeat( level ) ),
...createOptionsFromPostsWithHierarchy( children, prefix, level + 1 ),
] ).flat();
}
/**
* Create an options array to be used with SelectControl for the given array of term objects.
*
* @param {object[]} terms - Array with term objects as elements.
* @returns {object[]} Options data to use with SelectControl.
*/
export function createOptionsFromTerms( terms ) {
return terms.map( ( term ) => createOptionFromTerm( term ) );
}
/**
* Create an options array to be used with SelectControl for the given array of term objects, visualizing the hierarchy.
*
* @param {object[]} terms - Array with term objects as elements.
* @param {string} [prefix='— '] - Optional. Prefix to use for the label. Defaults to "— ".
* @param {number} [level=0] - Optional. Hierarchy level. Defaults to 0.
* @returns {object[]} Options data to use with SelectControl.
*/
export function createOptionsFromTermsWithHierarchy( terms, prefix = '— ', level = 0 ) {
return terms.map( ( { children = [], ...term } ) => [
createOptionFromTerm( term, prefix.repeat( level ) ),
...createOptionsFromTermsWithHierarchy( children, prefix, level + 1 ),
] ).flat();
}