jsadvent/lib/_pollute/string.js

31 lines
714 B
JavaScript
Raw Normal View History

2020-12-10 08:33:00 +00:00
const crypto = require('crypto');
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('');
};