jsadvent/lib/_pollute/string.js

49 lines
1 KiB
JavaScript

const crypto = require('crypto');
const Glib = require('../glib');
Object.defineProperty(String.prototype, 'lengthN', {
enumerable: false,
configurable: false,
get() {
return BigInt(this.length);
},
});
Object.defineProperty(String.prototype, 'glib', {
enumerable: false,
configurable: false,
get() {
return Glib.fromIterable(this);
},
});
Object.defineProperty(String.prototype, 'md5', {
enumerable: false,
configurable: false,
get() {
return crypto
.createHash('md5')
.update(this.valueOf())
.digest('hex');
},
});
Object.defineProperty(String.prototype, 'characterFrequencies', {
enumerable: false,
configurable: false,
get() {
const frequency = new Map();
for (const letter of this) {
frequency.set(letter, (frequency.get(letter) || BigInt(0)) + BigInt(1));
}
return frequency;
},
});
String.prototype.splice = function(...args) {
const characterArray = this.valueOf().split('');
characterArray.splice(...args);
return characterArray.join('');
};