Helping Tree-Shake
You will notice all examples import format
Object from Quasar. However, if you need only one formatter method from it, then you can use ES6 destructuring to help Tree Shaking embed only that method and not all of format
.
Example:
// we import all of `format`
import { format } from 'quasar'
// destructuring to keep only what is needed
const { capitalize, humanStorageSize } = format
console.log( capitalize('some text') )
// Some text
console.log( humanStorageSize(13087) )
// 12.8kB
content_paste
You can also import all formatters and use whatever you need like this (but note that your bundle will probably contain unused methods too):
import { format } from 'quasar'
console.log( format.capitalize('some text') )
console.log( format.humanStorageSize(13087) )
content_paste
TIP
For usage with the UMD build see here.
Capitalize
import { format } from 'quasar'
const { capitalize } = format
console.log( capitalize('some text') )
// Some text
content_paste
Format to Human Readable Size
import { format } from 'quasar'
const { humanStorageSize } = format
// humanStorageSize(value, decimals = 1)
// "decimals" param requires Quasar v2.15.3+
console.log( humanStorageSize(13087) )
// 12.8KB
console.log( humanStorageSize(1024 * 1024 * 2.25, 3) )
// 2.250MB
content_paste
Normalize Number to Interval
import { format } from 'quasar'
const { between } = format
// (Number) between(Number, Number min, Number max)
console.log( between(50, 10, 20) )
// 20
content_paste
import { format } from 'quasar'
const { normalizeToInterval } = format
// (Number) normalizeToInterval(Number, Number lower_margin, Number upper_margin)
console.log( normalizeToInterval(21, 10, 20) ) // 10
console.log( normalizeToInterval(33, 10, 20) ) // 11
console.log( normalizeToInterval(52, 10, 20) ) // 19
console.log( normalizeToInterval(5, 10, 16) ) // 12
content_paste
Pad String
import { format } from 'quasar'
const { pad } = format
// (String) pad(String toPad, Number length, String paddingCharacter)
// length is default 2
// paddingCharacter is default '0'
console.log( pad('2', 4) )
// '0002'
content_paste