去空格,优化addArray,removeArray性能

This commit is contained in:
kuangshen04 2023-10-08 15:19:27 +08:00
parent e0f9e919c7
commit 27ddea0730
1 changed files with 135 additions and 134 deletions

View File

@ -8417,206 +8417,207 @@
} }
}); });
/*Map prototype end*/ /*Map prototype end*/
Object.defineProperty(Array.prototype, "filterInD", { Object.defineProperty(Array.prototype,"filterInD",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function (pos) { value:function(pos){
if (typeof pos != 'string') pos = 'o'; if(typeof pos!='string') pos='o';
return this.filter(card => pos.includes(get.position(card, true))); return this.filter(card=>pos.includes(get.position(card,true)));
} }
}); });
Object.defineProperty(Array.prototype, "someInD", { Object.defineProperty(Array.prototype,"someInD",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function (pos) { value:function(pos){
if (typeof pos != 'string') pos = 'o'; if(typeof pos!='string') pos='o';
return this.some(card => pos.includes(get.position(card, true))); return this.some(card=>pos.includes(get.position(card,true)));
} }
}); });
Object.defineProperty(Array.prototype, "everyInD", { Object.defineProperty(Array.prototype,"everyInD",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function (pos) { value:function(pos){
if (typeof pos != 'string') pos = 'o'; if(typeof pos!='string') pos='o';
return this.every(card => pos.includes(get.position(card, true))); return this.every(card=>pos.includes(get.position(card,true)));
} }
}); });
/** /**
* @legacy Use `Array.prototype.includes(searchElement)` instead. *@legacy Use `Array.prototype.includes(searchElement)` instead.
*/ */
Object.defineProperty(Array.prototype, "contains", { Object.defineProperty(Array.prototype,"contains",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: Array.prototype.includes value:Array.prototype.includes
}); });
Object.defineProperty(Array.prototype, "containsSome", { Object.defineProperty(Array.prototype,"containsSome",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
return Array.from(arguments).some(i=>this.includes(i)); return Array.from(arguments).some(i=>this.includes(i));
} }
}); });
Object.defineProperty(Array.prototype, "containsAll", { Object.defineProperty(Array.prototype,"containsAll",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
return Array.from(arguments).every(i=>this.includes(i)); return Array.from(arguments).every(i=>this.includes(i));
} }
}); });
Object.defineProperty(Array.prototype, "add", { Object.defineProperty(Array.prototype,"add",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
for (const arg of arguments) { for(const arg of arguments){
if (this.contains(arg)) continue; if(this.contains(arg)) continue;
this.push(arg); this.push(arg);
} }
return this; return this;
} }
}); });
Object.defineProperty(Array.prototype, "addArray", { Object.defineProperty(Array.prototype,"addArray",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
return Array.prototype.add.apply(this,Array.from(arguments).flat()); for(const i of arguments) this.add(...i);
return this;
} }
}); });
Object.defineProperty(Array.prototype, "remove", { Object.defineProperty(Array.prototype,"remove",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
for(const item of arguments){ for(const item of arguments){
const pos = this.indexOf(item); const pos=this.indexOf(item);
if (pos == -1) continue; if(pos==-1) continue;
this.splice(pos, 1); this.splice(pos,1);
} }
return this; return this;
} }
}); });
Object.defineProperty(Array.prototype, "removeArray", { Object.defineProperty(Array.prototype,"removeArray",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
return Array.prototype.remove.apply(this,Array.from(arguments).flat()); for(const i of arguments) this.add(...i);
} }
}); });
Object.defineProperty(Array.prototype, "unique", { Object.defineProperty(Array.prototype,"unique",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
let uniqueArray = this.toUniqued(); let uniqueArray=[...new Set(this)];
this.length = uniqueArray.length; this.length=uniqueArray.length;
for (let i = 0; i < uniqueArray.length; i++) this[i] = uniqueArray[i]; for(let i=0;i<uniqueArray.length;i++) this[i]=uniqueArray[i];
return this; return this;
} }
}); });
Object.defineProperty(Array.prototype, "toUniqued", { Object.defineProperty(Array.prototype,"toUniqued",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
return [...new Set(this)]; return [...new Set(this)];
} }
}); });
Object.defineProperty(Array.prototype, "randomGet", { Object.defineProperty(Array.prototype,"randomGet",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
let arr = this.slice(0); let arr=this.slice(0);
arr.removeArray(Array.from(arguments)); arr.removeArray(Array.from(arguments));
return arr[Math.floor(Math.random() * arr.length)]; return arr[Math.floor(Math.random()*arr.length)];
} }
}); });
Object.defineProperty(Array.prototype, "randomGets", { Object.defineProperty(Array.prototype,"randomGets",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function (num) { value:function(num){
if (num > this.length) num = this.length; if(num>this.length) num=this.length;
let arr = this.slice(0); let arr=this.slice(0);
let list = []; let list=[];
for (let i = 0; i < num; i++) { for(let i=0;i<num;i++){
list.push(arr.splice(Math.floor(Math.random() * arr.length), 1)[0]); list.push(arr.splice(Math.floor(Math.random()*arr.length),1)[0]);
} }
return list; return list;
} }
}); });
Object.defineProperty(Array.prototype, "randomRemove", { Object.defineProperty(Array.prototype,"randomRemove",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function (num) { value:function(num){
if (typeof num == 'number') { if(typeof num=='number'){
let list = []; let list=[];
for (let i = 0; i < num; i++) { for(let i=0;i<num;i++){
if (!this.length) break; if(!this.length) break;
list.push(this.randomRemove()); list.push(this.randomRemove());
} }
return list; return list;
} }
return this.splice(Math.floor(Math.random() * this.length), 1)[0]; return this.splice(Math.floor(Math.random()*this.length),1)[0];
} }
}); });
Object.defineProperty(Array.prototype, "randomSort", { Object.defineProperty(Array.prototype,"randomSort",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function () { value:function(){
var list = []; let list=[];
while (this.length) { while(this.length){
list.push(this.randomRemove()); list.push(this.randomRemove());
} }
for (var i = 0; i < list.length; i++) { for(let i=0;i<list.length;i++){
this.push(list[i]); this.push(list[i]);
} }
return this; return this;
} }
}); });
Object.defineProperty(Array.prototype, "sortBySeat", { Object.defineProperty(Array.prototype,"sortBySeat",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function (target) { value:function(target){
lib.tempSortSeat = target; lib.tempSortSeat=target;
this.sort(lib.sort.seat); this.sort(lib.sort.seat);
delete lib.tempSortSeat; delete lib.tempSortSeat;
return this; return this;
} }
}); });
/** /**
* @description 从数组中寻找某个特征最大的且通过筛选的第一个元素 *@description 从数组中寻找某个特征最大的且通过筛选的第一个元素
*/ */
Object.defineProperty(Array.prototype, "maxBy", { Object.defineProperty(Array.prototype,"maxBy",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function (sortBy, filter) { value:function(sortBy,filter){
let list = this.filter(filter||(() => true)); let list=this.filter(filter||(()=>true));
if (sortBy && typeof sortBy == 'function') list.sort((a, b) => sortBy(a) - sortBy(b)); if(sortBy&&typeof sortBy=='function') list.sort((a,b)=>sortBy(a)-sortBy(b));
else list.sort(); else list.sort();
return list[list.length - 1]; return list[list.length-1];
} }
}); });
Object.defineProperty(Array.prototype, "minBy", { Object.defineProperty(Array.prototype,"minBy",{
configurable: true, configurable:true,
enumerable: false, enumerable:false,
writable: true, writable:true,
value: function (sortBy, filter) { value:function(sortBy,filter){
let list = this.filter(filter||(() => true)); let list=this.filter(filter||(()=>true));
if (sortBy && typeof sortBy == 'function') list.sort((a, b) => sortBy(a) - sortBy(b)); if(sortBy&&typeof sortBy=='function') list.sort((a,b)=>sortBy(a)-sortBy(b));
else list.sort(); else list.sort();
return list[0]; return list[0];
} }