jsadvent/lib/_pollute/string.js

49 lines
1 KiB
JavaScript
Raw Normal View History

2020-12-10 08:33:00 +00:00
const crypto = require('crypto');
2020-12-20 01:56:58 +00:00
const Glib = require('../glib');
2020-12-13 06:52:00 +00:00
Object.defineProperty(String.prototype, 'lengthN', {
enumerable: false,
configurable: false,
get() {
return BigInt(this.length);
},
});
2020-12-20 01:56:58 +00:00
Object.defineProperty(String.prototype, 'glib', {
enumerable: false,
configurable: false,
get() {
return Glib.fromIterable(this);
},
});
2020-12-10 08:33:00 +00:00
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('');
};