UNPKG

@sem1083/proto.extend

Version:

Extends Prototypes like String, Array, Objects etc.

47 lines (46 loc) 2.44 kB
'use strict'; const owoString = (string) => { string = string.replace(/(?:l|r)/g, 'w'); string = string.replace(/(?:L|R)/g, 'W'); string = string.replace(/n([aeiou])/g, 'ny$1'); string = string.replace(/N([aeiou])/g, 'Ny$1'); string = string.replace(/N([AEIOU])/g, 'Ny$1'); string = string.replace(/ove/g, 'uv'); return string; }; Object.assign(String.prototype, { shuffle() { var r = (a, b) => { return Math.random()>.5 ? -1 : 1; }; return this.split('').sort(r).join(""); }, randomCode(options) { if (!options) throw "Incorrect usage: Missing options! \"<String>.randomCode(options)\" Options: {length: Number, includeDigits: boolean}." if (!options.hasOwnProperty("length")) throw "Incorrect usage: {...length: undefined}, \"length\" should be defined!"; if (!Number(options.length)) throw "Incorrect usage: {...length: NaN}, \"length\" should be a Number!"; if (!isFinite(options.length) || isNaN(options.length)) throw "Incorrect usage: {...length: Infinity, NaN, -Infinity, null}, \"length\" should be a valid number!"; var characters; if (!options.hasOwnProperty("includeDigits")) options.includeDigits = false; if (options.hasOwnProperty("includeDigits") && options.includeDigits == true) characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789'; else characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; var charactersLength = characters.length; var string = this; for ( var i = 0; i < options.length; i++ ) { string += characters.charAt(Math.floor(Math.random() * charactersLength)); } return string; }, removeChar(char) { if (!char) throw "Incorrect usage: value \"char\" is not defined. <String>.removeChar(char)"; if (!"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split('').includes(char)) throw "Incorrect usage: \"char\" is not a valid Character!\nABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; return this.split('').filter(u => u !== char).join(""); }, invert() { var string = this.split('').reverse().join(''); return string.toString(); }, owofy() { return owoString(this); } });