Generate inline background-image CSS style based on provided URL.

backgroundImageStyle(input: string): Object
Since: 1.0.0
Parameters
input (string) Background image URL or web address.
Returns
Object: CSS style for the provided background image address.
Example
backgroundImageStyle( 'https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png' );

// => Object { backgroundImage: 'url(https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png)' }

Extracts block specific CSS class name generated by Gutenberg editor. By default the block CSS class name is prefixed with wp-block keyword.

blockClassName(input: string): string
Since: 1.0.0
Parameters
input (string) CSS class names assigned to the block.
Returns
string: CSS class name generated for the block.
Example
blockClassName( 'wp-block-prefix-spacer custom-class-name test-class-name' );

// => string 'wp-block-prefix-spacer'

Extracts selected block style slug from the provided block specific CSS class names.

blockStyleSlug(input: string): string
Since: 1.0.0
Parameters
input (string) CSS class names assigned to the block.
Returns
string: Style name associated or selected for this block.
Example
blockStyleSlug( 'wp-block-prefix-spacer is-style-fancy test-class-name' );

// => string 'fancy'

Converts a string (e.g. 'yes' or 'no') to a bool.

stringToBoolean(input: (string | number)): boolean
Since: 1.0.0
Parameters
input ((string | number)) String to convert. If a bool is passed it will be returned as-is.
Returns
boolean: Converted value.
Example
stringToBoolean( 'yes' );

// => boolean true

Converts a bool to a 'yes' or 'no'.

booleanToString(input: (boolean | string)): string
Since: 1.0.0
Parameters
input ((boolean | string)) Bool to convert. If a string is passed it will first be converted to a bool.
Returns
string: Converted value.
Example
booleanToString( true );

// => string 'yes'

Creates an object composed of the (nested) picked object properties.

deepPick(collection: Object, paths: Array): Object
Since: 1.0.0
Parameters
collection (Object) The source object.
paths (Array) The property paths to pick.
Returns
Object: Returns the new object.
Example
deepPick( { id: 1, title: { rendered: 'Hello world!' } }, [ 'title.rendered' ] );

// => Object { title: 'Hello world!' }

Generate dim CSS class name based on the provided ratio or opacity.

dimRatioClassName(input: number): string
Since: 1.0.0
Parameters
input (number) Dim ratio or opacity. [0-100]
Returns
string: CSS class name generated from the ratio input.
Example
dimRatioClassName( 30 );

// => string 'has-background-dim-30'

filterCollectionByPredicate

src/filterCollectionByPredicate/index.js

Iterates over elements of input and returns all entries from collection for which predicate returns truthy.

Notice that this function preserves the order of input in the result set and is thus most useful if a collection must be filtered in an order that is specified by another collection.

filterCollectionByPredicate(input: Array, collection: Array, predicate: Function): Array
Since: 1.0.0
Parameters
input (Array) The input to search for.
collection (Array) The collection to search elements in.
predicate (Function) The predicate function to be applied in every iteration.
Returns
Array: All matching entries in collection.
Example
filterCollectionByPredicate( [ 1, 3 ], [ { id: 1, content: {} }, { id: 2, content: {} }, { id: 3, content: {} } ] );

// => Array [ { id: 1, content: {} }, { id: 3, content: {} } ]

Generates corresponding CSS based on the provided focal point picker values. Example focal point picker value: { x: 0.5, y: 0.1 } Corresponding CSS: background-position: 50% 10%;

focalPointStyle(value: Object): string
Since: 1.0.0
Parameters
value (Object) Focal point object value.
Returns
string: Calculated X and Y position based on the focalpoint object provided.
Example
focalPointStyle( { x: 0.67, y: 0.65 } );

// => string '67% 65%'

Gets a formatted version of the post content provided.

formattedContent(input: string): string
Since: 1.0.0
Parameters
input (string) Post content.
Returns
string: Formatted post content.
Example
formattedContent( '<span class=\"amount\"><bdi><span class=\"currency\">&pound;<\/span>11.05<\/bdi><\/span>' );

// => string '<span class="amount"><bdi><span class="currency">£</span>11.05</bdi></span>'

Extracts email addresses from mailto href link.

getMailTo(input: string): Array
Since: 1.0.0
Parameters
input (string) The href attribute value.
Returns
Array: Extracted email-addresses from the provided href value.
Example
getMailTo( 'mailto:info@example.com,info@test.com' );

// => Array '[ 'info@example.com', 'info@test.com' ]'

Converts a given hex-unicode into an Emoji icon.

hexToEmoji(value: string): any
Since: 1.0.0
Parameters
value (string) Emoji specific hex code.
Returns
any: Rendered Emoji.
Example
hexToEmoji( '1F603' );

// => string 😃

Check if value is classified as an array object and is not empty.

ifArray(input: Array): boolean
Since: 1.0.0
Parameters
input (Array) The value to check.
Returns
boolean: Whether the argument provided is a non-empty array.
Example
ifArray( [] );

// => boolean false

Insert an item into an array at a specific index.

insertAtIndex(input: Array, value: any, index: number): Array
Since: 1.0.0
Parameters
input (Array) The array to add to.
value (any) The value to insert.
index (number) Given array index to insert the value.
Returns
Array: Returns the new updated array.
Example
insertAtIndex( [ 'a', 'd', 'c' ], 'b', 1 );

// => Array [ 'a', 'b', 'c' ]

Determine whether the content is center positioned.

isPositionCenter(input: string): boolean
Since: 1.0.0
Parameters
input (string) The value to check.
Returns
boolean: Return true if content is center positioned.
Example
isPositionCenter( 'center' );

// => boolean true

Parses a JSON string, constructing the JavaScript value or object described by the string.

jsonify(input: string): Object
Since: 1.0.0
Parameters
input (string) The string to parse as JSON.
Returns
Object: Converted value to object.
Example
jsonify( '[ { "id": 1, "title": "sunt aut facere" ,{ "id": 2, "title": "qui est esse" } ]' );

// => Object [ { id: 1, title: 'sunt aut facere' }, { id: 2, title: 'qui est esse' } ]

Determines which element has the highest occurrence (mode) in the given array.

mode(input: Array): string
Since: 1.0.0
Parameters
input (Array) The array to process.
Returns
string: Mode element.
Example
mode( [ 'pear', 'apple', 'orange', 'apple' ] );

// => string "apple"

Validate and normalize JSON string to an array.

normalizeJsonify(input: string): Array
Since: 1.0.0
Parameters
input (string) JSON string to be normalized to an array.
Returns
Array: Constructed JavaScript object from the given JSON string.
Example
const options = normalizeJsonify( '[\u0022Search Engine\u0022,\u0022Social Media\u0022]' );

// => Array [ 'Search Engine', 'Social Media' ]

Creates an object composed of the (nested) picked object properties.

pickRelevantMediaFiles(image: Object, sizeSlug: string): Object
Since: 1.4.0
Parameters
image (Object) Media object.
sizeSlug (string) The currently-selected image size slug (thumbnail, large, etc).
Returns
Object: Relevant properties extracted from the original given media object.
Example
pickRelevantMediaFiles( imageObject );

// => Object { alt: '', caption: '', id: "21", url: 'http://dev.local/wp-content/uploads/2022/09/image.jpg' }

Generates content-position CSS class name based on provided input.

positionToClassName(input: string): string
Since: 1.0.0
Parameters
input (string) CSS position input.
Returns
string: Corresponding CSS class name based on the provided position.
Example
positionToClassName( 'center right' );

// => string 'is-position-center-right'

A reducer is a function accepting the previous state and action as arguments and returns an updated state value.

reducer(state: Object, action: Object): Object
Since: 1.3.0
Parameters
state (Object) The destination object.
action (Object) The source objects.
Returns
Object: Returns object.
Example
const [ state, dispatch ] = useReducer( reducer, value );

Removes an item from the array at a specific index.

removeAtIndex(input: Array, index: number): Array
Since: 1.0.0
Parameters
input (Array) The array to remove from.
index (number) Given array index to remove the value from.
Returns
Array: Returns the new updated array.
Example
removeAtIndex( [ 'a', 'd', 'b' ], 1 );

// => Array [ 'a', 'b' ]

Sanitizes the slug value.

sanitizeSlug(input: string): string
Since: 1.1.0
Parameters
input (string) String to sanitize.
Returns
string: Sanitized value for the slug.
Example
sanitizeSlug( 'k e__#y@234' );

// => string k-e__-y-234

Generates an array of objects to be passed to the SelectControl component. SelectControl allow users to select from a single-option menu. It functions as a wrapper around the browser’s native <select> element.

selectOptions(posts: Array, paths: string, none: Array): Array
Since: 1.0.0
Parameters
posts (Array) The response on the API call of array of post objects.
paths (string) The property paths to pick.
none (Array) Text to display for showing no options being selected.
Returns
Array: An array of objects containing the label to be shown to the user, and value used to choose the selected value.
Example
selectOptions( [ { id: 1, title: { rendered: 'sunt aut facere' } }, { id: 2, title: { rendered: 'qui est esse' } } ], { id: 'value', 'title.rendered': 'label' } );

// => Array [ { value: 1, label: 'Hello world!' }, { value: 2, label: 'qui est esse' } ]

Generate shortcode.

shortcode(tagName: string, attributes: Object): string
Since: 1.0.0
Parameters
tagName (string) Shortcode tag name.
attributes (Object) Shortcode attributes.
Returns
string: Generated shortcode.
Example
shortcode( 'prefix_teams', { posts: [1, 2], number: 2 } );

// => string '[prefix_teams posts="1,2" number="2"]'

Slugifies every string, even when it contains unicode! Properly strips all HTML tags including script and style.

slugify(input: (Array | string)): string
Since: 1.0.0
Parameters
input ((Array | string)) The value to slugify.
Returns
string: Converted value to slug.
Example
slugify( 'unicode is ♥' );

// => string 'unicode-is-love'

Converts empty spaces to its corresponsding HTML character entity ( ). A non-breaking space is a space that will not break into a new line. Two words separated by a non-breaking space will stick together (not break into a new line). This method is handy when breaking the words might be disruptive.

spaceToNbsp(input: string): string
Since: 1.0.0
Parameters
input (string) String to convert.
Returns
string: Converted value.
Example
spaceToNbsp( 'Hello World! This is an example.' );

// => string 'Hello&nbsp;World!&nbsp;This&nbsp;is&nbsp;an&nbsp;example.'

Converts a JavaScript object or value to a JSON string.

stringify(input: Object): string
Since: 1.0.0
Parameters
input (Object) The value to convert to a JSON object.
Returns
string: Converted value to string.
Example
stringify( [ { id: 1, title: 'sunt aut facere' }, { id: 2, title: 'qui est esse' } ] );

// => string '[{"id":1,"title":"sunt aut facere"},{"id":2,"title":"qui est esse"}]'

Stringify an array of objects into a query string.

stringifyQuery(input: Array): string
Since: 1.0.0
Parameters
input (Array) The array of objects to query stringify.
Returns
string: Returns a stringified query.
Example
stringifyQuery( [ { product: '1885' }, { action: 'hook_name' } ] );

// => string 'product=1885&action=hook_name'