toBoolean()
This function is part of Iconify Utils package.
Function toBoolean() converts strings to boolean. It is used to parse various possible component property values to use them in IconCustomisations properties.
Usage
Function has the following parameters:
- name, string. Property name.
- value, unknown. Value to parse.
- defaultValue, boolean. Value to return if value cannot be parsed.
Function returns boolean value.
Example
demo.ts
ts
import { toBoolean } from '@iconify/utils';
/**
* Using React or Svelte syntax for attributes for demo
*
* For default value using the opposite of expected value to make sure that value has been parsed
*/
// Returns true
console.log(`Testing hFlip={true}:`, toBoolean('hFlip', true, false));
console.log(`Testing hFlip="true":`, toBoolean('hFlip', 'true', false));
console.log(`Testing hFlip="hFlip":`, toBoolean('hFlip', 'hFlip', false));
console.log(`Testing hFlip={1}:`, toBoolean('hFlip', 1, false));
// Returns false
console.log(`Testing hFlip={false}:`, toBoolean('hFlip', false, true));
console.log(`Testing hFlip={0}:`, toBoolean('hFlip', 0, true));
console.log(`Testing hFlip="false":`, toBoolean('hFlip', 'false', true));
console.log(`Testing hFlip="":`, toBoolean('hFlip', '', true));
Result:
Testing hFlip={true}: true
Testing hFlip="true": true
Testing hFlip="hFlip": true
Testing hFlip={1}: true
Testing hFlip={false}: false
Testing hFlip={0}: false
Testing hFlip="false": false
Testing hFlip="": false