This repository was archived by the owner on Mar 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (40 loc) · 1.55 KB
/
index.js
File metadata and controls
50 lines (40 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import charMap from './charmap';
const rxRemoveHash = /^#/;
const rxRemoveQuotes = /(^['"])|(['"]$)/g;
const rxAstralRange = / |\ud83c[\udffb-\udfff](?=\ud83c[\udffb-\udfff])|(?:[^\ud800-\udfff][\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]?|[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*/g;
export function length(str) {
const match = str.match(rxAstralRange);
return (match === null) ? 0 : match.length;
}
export function substring(str, begin, end) {
return str.match(rxAstralRange).slice(begin, end).join('')
}
export function removeHash(str) {
return str.trim().replace(rxRemoveHash, '');
}
export function removeQuotes(str) {
return str.replace(rxRemoveQuotes, '');
}
export function toCamelCase(str) {
return str.replace(/[-_](\w)/g, (matches, letter) => letter.toUpperCase());
}
export function pluralize(single, plural, opts = {}) {
return number => {
if (opts.zeroIsEmpty && !number) {
return '';
}
if (number === 1) {
return number + ' ' + single;
}
return (number || 0) + ' ' + plural;
}
}
export function slugify(str) {
return str
.toLowerCase()
.split('')
.map(char => charMap[char] || char)
.join('')
.replace(/ /ig, '-')
.replace(/[^-a-z0-9]{1,60}/ig, '');
}