๐String extension
JS โฉ value โฉ primitive โฉ String โฉ extension
add custom methods to String.prototype.
// ๐ธ str.characters - iterable iterator of characters
// ๐ธ str.codePoints - array of code points
// ๐ธ str.codeUnits - array of code units
// ๐น str.replaceWhitespaces()
replit โฉ String extension
// โญ install "grapheme-splitter": `npm install grapheme-splitter`
// 2023.01.03 - 19:40 + .codeUnits, .codePoints (support `GraphemeSplitter`)
//
// โญ String extension
// -------------------------------------------------------------------
// ๐ธ str.characters - iterable iterator of characters
// ๐ธ str.codePoints - array of code points
// ๐ธ str.codeUnits - array of code units
// ๐น str.replaceWhitespaces()
// -------------------------------------------------------------------
const { range } = require('./Iterable.js'); // iterable methods
// โญ import 'grapheme-splitter'
const GraphemeSplitter = require('grapheme-splitter');
const splitter = new GraphemeSplitter();
// -------------------------------------------------------------------
// String extension
Object.defineProperties(String.prototype, {
// ๐น str.replaceWhitespaces()
replaceWhitespaces: {
value: function(str = ',') {
return this
.trim() // โญ๏ธ removes whitespace from both ends
.replace(/\s+/g, str);
},
},
// ๐ธ str.characters
// - real characters (grapheme clusters, may contain many code points)
characters: {
get: function() {
// string -> iterable iterator
return splitter.iterateGraphemes(this);
},
},
// ๐ธ str.codePoints
// - 1 code point may contain 1 ~ 2 code units.
codePoints: {
get: function() {
let codePoints = [];
for (const char of this) { // an iterator of "code points"
codePoints.push(char.codePointAt(0));
}
return codePoints;
},
},
// ๐ธ str.codeUnits
// - 16-bit code units
codeUnits: {
get: function() {
return [...range(0, this.length - 1).map(i => this.charCodeAt(i))];
},
},
});
// export
module.exports = {};
const _Number = require('./ext/Number_ext.js'); // Number extension
const _String = require('./ext/String_ext.js'); // String extension
const { range } = require('./ext/Iterable.js'); // iterable methods
// ---------------------------------------------------------------------------
// โญ import 'grapheme-splitter'
const GraphemeSplitter = require('grapheme-splitter');
const ilove = 'Iโค๏ธ๐ณ๏ธโ๐';
// `.characters` iterates over "grapheme clusters".
ilove.characters.array, // [ 'I', 'โค๏ธ', '๐ณ๏ธโ๐' ] (3 characters)
ilove.characters.map(ch => ch.codePoints).array,
// [ [ 73 ], [ 10084, 65039 ], [ 127987, 65039, 8205, 127752 ] ] (7 code points)
// [ 'โค' , ? ] [ '๐ณ' , ?, ?, '๐' ]
// โฐโIโโโฏ โฐโโโโโ โค๏ธ โโโโโโฏ โฐโโโโโโโโโโโโ ๐ณ๏ธโ๐ โโโโโโโโโโโโโโฏ
// `str.map` iterates over "code points"โ
// code point -> 1 or 2 code units
ilove.map(cp => cp.codeUnits).array, // (9 code units)
// [
// [ 73 ],
// [ 10084 ],
// [ 65039 ],
// [ 55356, 57331 ], // surrogate pair
// [ 65039 ],
// [ 8205 ],
// [ 55356, 57096 ] // surrogate pair
// ]
str.isKeyword - check if a string is a keyword
str.isReservedWord - check if a string a reserved word
str.isPunctuator - check if a string is a punctuator
Unicode - about code points, code units ...
Last updated