@avidian/extras
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -9,36 +9,110 @@ "use strict"; | ||
var lodash_1 = require("lodash"); | ||
Array.prototype.random = function () { | ||
return this[Math.floor(Math.random() * this.length)]; | ||
}; | ||
Array.prototype.first = function () { | ||
if (this.length > 0) { | ||
return this[0]; | ||
} | ||
return null; | ||
}; | ||
Array.prototype.last = function () { | ||
if (this.length > 0) { | ||
return this[this.length - 1]; | ||
} | ||
return null; | ||
}; | ||
Array.prototype.flatten = function () { | ||
return lodash_1.flattenDeep(this); | ||
}; | ||
Array.prototype.groupBy = function (key) { | ||
var temp = {}; | ||
this.forEach(function (item) { | ||
var property = item[key]; | ||
if (!(property in temp)) { | ||
temp[property] = []; | ||
} | ||
temp[property].push(item); | ||
var errors = []; | ||
if (typeof Array.prototype.random === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'random', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return this[Math.floor(Math.random() * this.length)]; | ||
}, | ||
}); | ||
return Object.keys(temp).map(function (key) { return temp[key]; }); | ||
}; | ||
Array.prototype.except = function (keys) { | ||
return __spreadArray([], this).map(function (item) { return item.except(keys); }); | ||
}; | ||
Array.prototype.only = function (keys) { | ||
return __spreadArray([], this).map(function (item) { return item.only(keys); }); | ||
}; | ||
} | ||
else { | ||
errors.push('random'); | ||
} | ||
if (typeof Array.prototype.first === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'first', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
if (this.length > 0) { | ||
return this[0]; | ||
} | ||
return null; | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('first'); | ||
} | ||
if (typeof Array.prototype.last === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'last', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
if (this.length > 0) { | ||
return this[this.length - 1]; | ||
} | ||
return null; | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('last'); | ||
} | ||
if (typeof Array.prototype.flatten === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'flatten', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return lodash_1.flattenDeep(this); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('flatten'); | ||
} | ||
if (typeof Array.prototype.groupBy === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'groupBy', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function (key) { | ||
var temp = {}; | ||
this.forEach(function (item) { | ||
var property = item[key]; | ||
if (!(property in temp)) { | ||
temp[property] = []; | ||
} | ||
temp[property].push(item); | ||
}); | ||
return Object.keys(temp).map(function (key) { return temp[key]; }); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('groupBy'); | ||
} | ||
if (typeof Array.prototype.except === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'except', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function (keys) { | ||
return __spreadArray([], this).map(function (item) { return item.except(keys); }); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('except'); | ||
} | ||
if (typeof Array.prototype.only === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'only', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function (keys) { | ||
return __spreadArray([], this).map(function (item) { return item.getOnly(keys); }); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('only'); | ||
} | ||
if (errors.length > 0) { | ||
console.error("@avidian/extras:Array: Unable to patch the following methods - " + errors.join(', ')); | ||
} |
@@ -9,7 +9,31 @@ "use strict"; | ||
dayjs_1.default.extend(relativeTime_1.default); | ||
Date.prototype.toDayJS = function () { | ||
return dayjs_1.default(this); | ||
}; | ||
Date.prototype.fromNow = function () { | ||
return this.toDayJS().fromNow(); | ||
}; | ||
var errors = []; | ||
if (typeof Date.prototype.toDayJS === 'undefined') { | ||
Object.defineProperty(Date.prototype, 'toDayJS', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return dayjs_1.default(this); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('toDayJS'); | ||
} | ||
if (typeof Date.prototype.fromNow === 'undefined') { | ||
Object.defineProperty(Date.prototype, 'fromNow', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return this.toDayJS().fromNow(); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('fromNow'); | ||
} | ||
if (errors.length > 0) { | ||
console.error("@avidian/extras:Date: Unable to patch the following methods - " + errors.join(', ')); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
Error.prototype.toJSON = function () { | ||
var alt = {}; | ||
var _this = this; | ||
Object.getOwnPropertyNames(_this).forEach(function (key) { | ||
alt[key] = _this[key]; | ||
}, _this); | ||
if ('stack' in alt) { | ||
alt.stack = alt.stack | ||
.split(/\r?\n/) | ||
.map(function (string) { return string.trim(); }) | ||
.filter(function (_, i) { return i !== 0; }); | ||
} | ||
return alt; | ||
}; | ||
var errors = []; | ||
if (typeof Error.prototype.toJSON === 'undefined') { | ||
Object.defineProperty(Error.prototype, 'toJSON', { | ||
writable: false, | ||
enumerable: false, | ||
configurable: false, | ||
value: function () { | ||
var alt = {}; | ||
var _this = this; | ||
Object.getOwnPropertyNames(_this).forEach(function (key) { | ||
alt[key] = _this[key]; | ||
}, _this); | ||
if ('stack' in alt) { | ||
alt.stack = alt.stack | ||
.split(/\r?\n/) | ||
.map(function (string) { return string.trim(); }) | ||
.filter(function (_, i) { return i !== 0; }); | ||
} | ||
return alt; | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('toJSON'); | ||
} | ||
if (errors.length > 0) { | ||
console.error("@avidian/extras:Error: Unable to patch the following methods - " + errors.join(', ')); | ||
} |
@@ -26,3 +26,3 @@ import 'core-js'; | ||
except<T = any>(keys: string[]): T; | ||
only<T = any>(keys: string[]): T; | ||
getOnly<T = any>(keys: string[]): T; | ||
copy<T = any>(): T; | ||
@@ -29,0 +29,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
Object.prototype.serialize = function () { | ||
return JSON.stringify(this); | ||
}; | ||
Object.prototype.except = function (keys) { | ||
var copy = {}; | ||
for (var key in this) { | ||
if (!keys.includes(key)) { | ||
copy[key] = this[key]; | ||
} | ||
} | ||
return copy; | ||
}; | ||
Object.prototype.only = function (keys) { | ||
var result = {}; | ||
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { | ||
var key = keys_1[_i]; | ||
result[key] = this[key]; | ||
} | ||
return result; | ||
}; | ||
Object.prototype.copy = function () { | ||
return JSON.parse(JSON.stringify(this)); | ||
}; | ||
var errors = []; | ||
if (typeof Object.prototype.serialize === 'undefined') { | ||
Object.defineProperty(Object.prototype, 'serialize', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return JSON.stringify(this); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('serialize'); | ||
} | ||
if (typeof Object.prototype.except) { | ||
Object.defineProperty(Object.prototype, 'except', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function (keys) { | ||
var copy = {}; | ||
for (var key in this) { | ||
if (!keys.includes(key)) { | ||
copy[key] = this[key]; | ||
} | ||
} | ||
return copy; | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('except'); | ||
} | ||
if (typeof Object.prototype.getOnly === 'undefined') { | ||
Object.defineProperty(Object.prototype, 'getOnly', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function (keys) { | ||
var result = {}; | ||
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { | ||
var key = keys_1[_i]; | ||
result[key] = this[key]; | ||
} | ||
return result; | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('getOnly'); | ||
} | ||
if (typeof Object.prototype.copy === 'undefined') { | ||
Object.defineProperty(Object.prototype, 'copy', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return JSON.parse(JSON.stringify(this)); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('copy'); | ||
} | ||
if (errors.length > 0) { | ||
console.error("@avidian/extras:Object: Unable to patch the following methods - " + errors.join(', ')); | ||
} |
@@ -10,39 +10,110 @@ "use strict"; | ||
dayjs_1.default.extend(relativeTime_1.default); | ||
String.prototype.toNumber = function () { | ||
var parts = this.split('.'); | ||
if (parts.length > 1) { | ||
var whole = (parts[0].match(/\d/g) || []).join(''); | ||
var decimals = (parts[1].match(/\d/g) || []).join(''); | ||
return Number(whole + "." + decimals) || 0; | ||
} | ||
var match = this.match(/\d/g); | ||
if (!match) { | ||
return 0; | ||
} | ||
return Number(match.join('')) || 0; | ||
}; | ||
String.prototype.trim = function () { | ||
return lodash_1.trim(this.toString()); | ||
}; | ||
String.prototype.toDayJS = function () { | ||
var instance = dayjs_1.default(this.toString()); | ||
if (!instance.isValid()) { | ||
throw new Error('Invalid Date'); | ||
} | ||
return instance; | ||
}; | ||
String.prototype.toDate = function () { | ||
return this.toDayJS().toDate(); | ||
}; | ||
String.prototype.fromNow = function () { | ||
return this.toDayJS().fromNow(); | ||
}; | ||
String.random = function (size) { | ||
if (size === void 0) { size = 40; } | ||
var characters = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'; | ||
var results = ''; | ||
for (var x = 0; x < size; x++) { | ||
results += characters.charAt(Math.floor(Math.random() * characters.length)); | ||
} | ||
return results; | ||
}; | ||
var errors = []; | ||
if (typeof String.prototype.toNumber === 'undefined') { | ||
Object.defineProperty(String.prototype, 'toNumber', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
var parts = this.split('.'); | ||
if (parts.length > 1) { | ||
var whole = (parts[0].match(/\d/g) || []).join(''); | ||
var decimals = (parts[1].match(/\d/g) || []).join(''); | ||
return Number(whole + "." + decimals) || 0; | ||
} | ||
var match = this.match(/\d/g); | ||
if (!match) { | ||
return 0; | ||
} | ||
return Number(match.join('')) || 0; | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('toNumber'); | ||
} | ||
if (typeof String.prototype.trim === 'undefined') { | ||
Object.defineProperty(String.prototype, 'trim', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return lodash_1.trim(this.toString()); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('trim'); | ||
} | ||
if (typeof String.prototype.toDayJS === 'undefined') { | ||
Object.defineProperty(String.prototype, 'toDayJS', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
var instance = dayjs_1.default(this.toString()); | ||
if (!instance.isValid()) { | ||
throw new Error('Invalid Date'); | ||
} | ||
return instance; | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('toDayJS'); | ||
} | ||
if (typeof String.prototype.toDate === 'undefined') { | ||
Object.defineProperty(String.prototype, 'toDate', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
var instance = dayjs_1.default(this.toString()); | ||
if (!instance.isValid()) { | ||
throw new Error('Invalid Date'); | ||
} | ||
return instance.toDate(); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('toDate'); | ||
} | ||
if (typeof String.prototype.fromNow === 'undefined') { | ||
Object.defineProperty(String.prototype, 'fromNow', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
var instance = dayjs_1.default(this.toString()); | ||
if (!instance.isValid()) { | ||
throw new Error('Invalid Date'); | ||
} | ||
return instance.fromNow(); | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('fromNow'); | ||
} | ||
if (typeof String.random === 'undefined') { | ||
Object.defineProperty(String, 'random', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function (size) { | ||
var characters = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'; | ||
var results = ''; | ||
for (var x = 0; x < size; x++) { | ||
results += characters.charAt(Math.floor(Math.random() * characters.length)); | ||
} | ||
return results; | ||
}, | ||
}); | ||
} | ||
else { | ||
errors.push('random'); | ||
} | ||
if (errors.length > 0) { | ||
console.error("@avidian/extras:String: Unable to patch the following methods - " + errors.join(', ')); | ||
} |
{ | ||
"name": "@avidian/extras", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "Extra features on native objects.", |
137
src/Array.ts
import { flattenDeep } from 'lodash'; | ||
Array.prototype.random = function () { | ||
return this[Math.floor(Math.random() * this.length)]; | ||
}; | ||
const errors: string[] = []; | ||
Array.prototype.first = function () { | ||
if (this.length > 0) { | ||
return this[0]; | ||
} | ||
return null; | ||
}; | ||
if (typeof Array.prototype.random === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'random', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return this[Math.floor(Math.random() * this.length)]; | ||
}, | ||
}); | ||
} else { | ||
errors.push('random'); | ||
} | ||
Array.prototype.last = function () { | ||
if (this.length > 0) { | ||
return this[this.length - 1]; | ||
} | ||
return null; | ||
}; | ||
if (typeof Array.prototype.first === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'first', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
if (this.length > 0) { | ||
return this[0]; | ||
} | ||
return null; | ||
}, | ||
}); | ||
} else { | ||
errors.push('first'); | ||
} | ||
Array.prototype.flatten = function () { | ||
return flattenDeep(this); | ||
}; | ||
if (typeof Array.prototype.last === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'last', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
if (this.length > 0) { | ||
return this[this.length - 1]; | ||
} | ||
return null; | ||
}, | ||
}); | ||
} else { | ||
errors.push('last'); | ||
} | ||
Array.prototype.groupBy = function <T, K extends keyof T>(key: K) { | ||
const temp: { [key: string]: Array<T> } = {}; | ||
if (typeof Array.prototype.flatten === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'flatten', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return flattenDeep(this); | ||
}, | ||
}); | ||
} else { | ||
errors.push('flatten'); | ||
} | ||
this.forEach((item) => { | ||
const property: any = item[key]; | ||
if (!(property in temp)) { | ||
temp[property] = []; | ||
} | ||
temp[property].push(item); | ||
if (typeof Array.prototype.groupBy === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'groupBy', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function <T, K extends keyof T>(key: K) { | ||
const temp: { [key: string]: Array<T> } = {}; | ||
this.forEach((item: any) => { | ||
const property: any = item[key]; | ||
if (!(property in temp)) { | ||
temp[property] = []; | ||
} | ||
temp[property].push(item); | ||
}); | ||
return Object.keys(temp).map((key) => temp[key]); | ||
}, | ||
}); | ||
return Object.keys(temp).map((key) => temp[key]); | ||
}; | ||
} else { | ||
errors.push('groupBy'); | ||
} | ||
Array.prototype.except = function <T, K extends keyof T>(keys: K[]) { | ||
return [...this].map((item: Object) => item.except(keys as any)); | ||
}; | ||
if (typeof Array.prototype.except === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'except', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function <T, K extends keyof T>(keys: K[]) { | ||
return [...this].map((item: object) => item.except(keys as any)); | ||
}, | ||
}); | ||
} else { | ||
errors.push('except'); | ||
} | ||
Array.prototype.only = function <T, K extends keyof T>(keys: K[]) { | ||
return [...this].map((item: Object) => item.only(keys as any)); | ||
}; | ||
if (typeof Array.prototype.only === 'undefined') { | ||
Object.defineProperty(Array.prototype, 'only', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function <T, K extends keyof T>(keys: K[]) { | ||
return [...this].map((item: Object) => item.getOnly(keys as any)); | ||
}, | ||
}); | ||
} else { | ||
errors.push('only'); | ||
} | ||
if (errors.length > 0) { | ||
console.error(`@avidian/extras:Array: Unable to patch the following methods - ${errors.join(', ')}`); | ||
} | ||
export {}; |
@@ -6,8 +6,34 @@ import dayjs from 'dayjs'; | ||
Date.prototype.toDayJS = function () { | ||
return dayjs(this); | ||
}; | ||
const errors: string[] = []; | ||
Date.prototype.fromNow = function () { | ||
return this.toDayJS().fromNow(); | ||
}; | ||
if (typeof Date.prototype.toDayJS === 'undefined') { | ||
Object.defineProperty(Date.prototype, 'toDayJS', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return dayjs(this); | ||
}, | ||
}); | ||
} else { | ||
errors.push('toDayJS'); | ||
} | ||
if (typeof Date.prototype.fromNow === 'undefined') { | ||
Object.defineProperty(Date.prototype, 'fromNow', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return this.toDayJS().fromNow(); | ||
}, | ||
}); | ||
} else { | ||
errors.push('fromNow'); | ||
} | ||
if (errors.length > 0) { | ||
console.error(`@avidian/extras:Date: Unable to patch the following methods - ${errors.join(', ')}`); | ||
} | ||
export {}; |
@@ -1,19 +0,34 @@ | ||
Error.prototype.toJSON = function () { | ||
const alt = {} as any; | ||
const errors: string[] = []; | ||
const _this = this as any; | ||
Object.getOwnPropertyNames(_this).forEach(function (key) { | ||
alt[key] = _this[key]; | ||
}, _this); | ||
if (typeof Error.prototype.toJSON === 'undefined') { | ||
Object.defineProperty(Error.prototype, 'toJSON', { | ||
writable: false, | ||
enumerable: false, | ||
configurable: false, | ||
value: function () { | ||
const alt = {} as any; | ||
if ('stack' in alt) { | ||
alt.stack = alt.stack | ||
.split(/\r?\n/) | ||
.map((string: string) => string.trim()) | ||
.filter((_: any, i: number) => i !== 0); | ||
} | ||
const _this = this as any; | ||
Object.getOwnPropertyNames(_this).forEach(function (key) { | ||
alt[key] = _this[key]; | ||
}, _this); | ||
return alt; | ||
}; | ||
if ('stack' in alt) { | ||
alt.stack = alt.stack | ||
.split(/\r?\n/) | ||
.map((string: string) => string.trim()) | ||
.filter((_: any, i: number) => i !== 0); | ||
} | ||
return alt; | ||
}, | ||
}); | ||
} else { | ||
errors.push('toJSON'); | ||
} | ||
if (errors.length > 0) { | ||
console.error(`@avidian/extras:Error: Unable to patch the following methods - ${errors.join(', ')}`); | ||
} | ||
export {}; |
@@ -30,3 +30,3 @@ import 'core-js'; | ||
except<T = any>(keys: string[]): T; | ||
only<T = any>(keys: string[]): T; | ||
getOnly<T = any>(keys: string[]): T; | ||
copy<T = any>(): T; | ||
@@ -33,0 +33,0 @@ } |
@@ -1,28 +0,70 @@ | ||
Object.prototype.serialize = function () { | ||
return JSON.stringify(this); | ||
}; | ||
const errors: string[] = []; | ||
Object.prototype.except = function <T, K extends keyof T>(keys: K[]) { | ||
const copy = {} as any; | ||
if (typeof Object.prototype.serialize === 'undefined') { | ||
Object.defineProperty(Object.prototype, 'serialize', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return JSON.stringify(this); | ||
}, | ||
}); | ||
} else { | ||
errors.push('serialize'); | ||
} | ||
for (const key in this) { | ||
if (!keys.includes(key as any)) { | ||
copy[key] = (this as any)[key]; | ||
} | ||
} | ||
return copy; | ||
}; | ||
if (typeof Object.prototype.except) { | ||
Object.defineProperty(Object.prototype, 'except', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function <T, K extends keyof T>(keys: K[]) { | ||
const copy = {} as any; | ||
Object.prototype.only = function <T = any>(keys: string[]) { | ||
const result = {} as any; | ||
for (const key of keys) { | ||
result[key] = (this as any)[key]; | ||
} | ||
return result; | ||
}; | ||
for (const key in this) { | ||
if (!keys.includes(key as any)) { | ||
copy[key] = (this as any)[key]; | ||
} | ||
} | ||
return copy; | ||
}, | ||
}); | ||
} else { | ||
errors.push('except'); | ||
} | ||
Object.prototype.copy = function () { | ||
return JSON.parse(JSON.stringify(this)); | ||
}; | ||
if (typeof Object.prototype.getOnly === 'undefined') { | ||
Object.defineProperty(Object.prototype, 'getOnly', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function (keys: string[]) { | ||
const result = {} as any; | ||
for (const key of keys) { | ||
result[key] = (this as any)[key]; | ||
} | ||
return result; | ||
}, | ||
}); | ||
} else { | ||
errors.push('getOnly'); | ||
} | ||
if (typeof Object.prototype.copy === 'undefined') { | ||
Object.defineProperty(Object.prototype, 'copy', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return JSON.parse(JSON.stringify(this)); | ||
}, | ||
}); | ||
} else { | ||
errors.push('copy'); | ||
} | ||
if (errors.length > 0) { | ||
console.error(`@avidian/extras:Object: Unable to patch the following methods - ${errors.join(', ')}`); | ||
} | ||
export {}; |
@@ -7,47 +7,115 @@ import dayjs from 'dayjs'; | ||
String.prototype.toNumber = function () { | ||
const parts = this.split('.'); | ||
if (parts.length > 1) { | ||
const whole = (parts[0].match(/\d/g) || []).join(''); | ||
const decimals = (parts[1].match(/\d/g) || []).join(''); | ||
return Number(`${whole}.${decimals}`) || 0; | ||
} | ||
const match = this.match(/\d/g); | ||
if (!match) { | ||
return 0; | ||
} | ||
return Number(match.join('')) || 0; | ||
}; | ||
const errors: string[] = []; | ||
String.prototype.trim = function () { | ||
return trim(this.toString()); | ||
}; | ||
if (typeof String.prototype.toNumber === 'undefined') { | ||
Object.defineProperty(String.prototype, 'toNumber', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
const parts = this.split('.'); | ||
if (parts.length > 1) { | ||
const whole = (parts[0].match(/\d/g) || []).join(''); | ||
const decimals = (parts[1].match(/\d/g) || []).join(''); | ||
return Number(`${whole}.${decimals}`) || 0; | ||
} | ||
const match = this.match(/\d/g); | ||
if (!match) { | ||
return 0; | ||
} | ||
return Number(match.join('')) || 0; | ||
}, | ||
}); | ||
} else { | ||
errors.push('toNumber'); | ||
} | ||
String.prototype.toDayJS = function () { | ||
const instance = dayjs(this.toString()); | ||
if (!instance.isValid()) { | ||
throw new Error('Invalid Date'); | ||
} | ||
return instance; | ||
}; | ||
if (typeof String.prototype.trim === 'undefined') { | ||
Object.defineProperty(String.prototype, 'trim', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
return trim(this.toString()); | ||
}, | ||
}); | ||
} else { | ||
errors.push('trim'); | ||
} | ||
String.prototype.toDate = function () { | ||
return this.toDayJS().toDate(); | ||
}; | ||
if (typeof String.prototype.toDayJS === 'undefined') { | ||
Object.defineProperty(String.prototype, 'toDayJS', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
const instance = dayjs(this.toString()); | ||
if (!instance.isValid()) { | ||
throw new Error('Invalid Date'); | ||
} | ||
return instance; | ||
}, | ||
}); | ||
} else { | ||
errors.push('toDayJS'); | ||
} | ||
String.prototype.fromNow = function () { | ||
return this.toDayJS().fromNow(); | ||
}; | ||
if (typeof String.prototype.toDate === 'undefined') { | ||
Object.defineProperty(String.prototype, 'toDate', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
const instance = dayjs(this.toString()); | ||
if (!instance.isValid()) { | ||
throw new Error('Invalid Date'); | ||
} | ||
return instance.toDate(); | ||
}, | ||
}); | ||
} else { | ||
errors.push('toDate'); | ||
} | ||
String.random = (size = 40) => { | ||
const characters = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'; | ||
let results = ''; | ||
if (typeof String.prototype.fromNow === 'undefined') { | ||
Object.defineProperty(String.prototype, 'fromNow', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function () { | ||
const instance = dayjs(this.toString()); | ||
if (!instance.isValid()) { | ||
throw new Error('Invalid Date'); | ||
} | ||
return instance.fromNow(); | ||
}, | ||
}); | ||
} else { | ||
errors.push('fromNow'); | ||
} | ||
for (let x = 0; x < size; x++) { | ||
results += characters.charAt(Math.floor(Math.random() * characters.length)); | ||
} | ||
if (typeof String.random === 'undefined') { | ||
Object.defineProperty(String, 'random', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: function (size: number) { | ||
const characters = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'; | ||
let results = ''; | ||
return results; | ||
}; | ||
for (let x = 0; x < size; x++) { | ||
results += characters.charAt(Math.floor(Math.random() * characters.length)); | ||
} | ||
return results; | ||
}, | ||
}); | ||
} else { | ||
errors.push('random'); | ||
} | ||
if (errors.length > 0) { | ||
console.error(`@avidian/extras:String: Unable to patch the following methods - ${errors.join(', ')}`); | ||
} | ||
export {}; |
@@ -22,3 +22,3 @@ import '../index'; | ||
const except = data.only(['name']); | ||
const except = data.getOnly(['name']); | ||
@@ -25,0 +25,0 @@ assert(!('age' in except)); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38449
1016
25