Skip to content

Instantly share code, notes, and snippets.

@gglnx
Created March 18, 2019 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gglnx/b5b179c629743c7427ebded67c0bd9bf to your computer and use it in GitHub Desktop.
Save gglnx/b5b179c629743c7427ebded67c0bd9bf to your computer and use it in GitHub Desktop.
Get unicode-range (CSS) from string
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