Created
March 18, 2019 16:29
-
-
Save gglnx/b5b179c629743c7427ebded67c0bd9bf to your computer and use it in GitHub Desktop.
Get unicode-range (CSS) from string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const multirange = require('multi-integer-range').multirange; | |
// Helpers | |
const trimLeadingZero = (str) => str[0] === '0' ? str.slice(str.lastIndexOf('0') + 1) : str; | |
const formatCodePoint = (...values) => `U+${values.map(trimLeadingZero).map(s => s.toString(16)).join('-')}`; | |
const uniqs = (arrArg) => arrArg.filter((elem, pos, arr) => arr.indexOf(elem) == pos); | |
// String for translate | |
const string = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¢£¥¨©«®´¸»ÄÖÜßäöüˆ˚˜–—‘’‚“”„…‹›€™'; | |
// Get all code points for string | |
const codePoints = uniqs(string.split('').map(c => c.codePointAt(0))); | |
// Combine into ranges | |
const ranges = multirange(codePoints); | |
// Translate into unicode-range | |
const unicodeRange = ranges.getRanges().map(range => { | |
return `${formatCodePoint(range[0])}${range[0] !== range[1] ? '-' + formatCodePoint(range[1]) : ''}`; | |
}); | |
console.log(unicodeRange.join(', ')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment