Merge pull request #487 from kuangshen04/PR-Branch

`Array.prototype`更改
This commit is contained in:
Spmario233 2023-10-08 23:13:39 +08:00 committed by GitHub
commit b4f1dabbca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 151 additions and 99 deletions

View File

@ -8435,6 +8435,15 @@
return this.some(card=>pos.includes(get.position(card,true)));
}
});
Object.defineProperty(Array.prototype,"everyInD",{
configurable:true,
enumerable:false,
writable:true,
value:function(pos){
if(typeof pos!='string') pos='o';
return this.every(card=>pos.includes(get.position(card,true)));
}
});
/**
*@legacy Use `Array.prototype.includes(searchElement)` instead.
*/
@ -8444,16 +8453,31 @@
writable:true,
value:Array.prototype.includes
});
Object.defineProperty(Array.prototype,"containsSome",{
configurable:true,
enumerable:false,
writable:true,
value:function(){
return Array.from(arguments).some(i=>this.includes(i));
}
});
Object.defineProperty(Array.prototype,"containsAll",{
configurable:true,
enumerable:false,
writable:true,
value:function(){
return Array.from(arguments).every(i=>this.includes(i));
}
});
Object.defineProperty(Array.prototype,"add",{
configurable:true,
enumerable:false,
writable:true,
value:function(){
for(var i=0;i<arguments.length;i++){
if(this.contains(arguments[i])){
return false;
}
this.push(arguments[i]);
for(const arg of arguments){
if(this.contains(arg)) continue;
this.push(arg);
}
return this;
}
@ -8462,10 +8486,8 @@
configurable:true,
enumerable:false,
writable:true,
value:function(arr){
for(var i=0;i<arr.length;i++){
this.add(arr[i]);
}
value:function(){
for(const i of Array.from(arguments)) this.add(...i);
return this;
}
});
@ -8473,16 +8495,12 @@
configurable:true,
enumerable:false,
writable:true,
value:function(item){
/*if(Array.isArray(item)){
for(var i=0;i<item.length;i++) this.remove(item[i]);
return;
}*/
var pos=this.indexOf(item);
if(pos==-1){
return false;
}
value:function(){
for(const item of arguments){
const pos=this.indexOf(item);
if(pos==-1) continue;
this.splice(pos,1);
}
return this;
}
});
@ -8490,76 +8508,85 @@
configurable:true,
enumerable:false,
writable:true,
value:function(arr){
for(var i=0;i<arr.length;i++){
this.remove(arr[i]);
}
value:function(){
for(const i of Array.from(arguments)) this.remove(...i);
return this;
}
});
Object.defineProperty(Array.prototype,"unique",{
configurable:true,
enumerable:false,
writable:true,
value:function(){
let uniqueArray=[...new Set(this)];
this.length=uniqueArray.length;
for(let i=0;i<uniqueArray.length;i++) this[i]=uniqueArray[i];
return this;
}
});
Object.defineProperty(Array.prototype,"toUniqued",{
configurable:true,
enumerable:false,
writable:true,
value:function(){
return [...new Set(this)];
}
});
Object.defineProperty(Array.prototype,"randomGet",{
configurable:true,
enumerable:false,
writable:true,
value:function(){
var arr=this.slice(0);
for(var i=0;i<arguments.length;i++) arr.remove(arguments[i]);
let arr=this.slice(0);
arr.removeArray(Array.from(arguments));
return arr[Math.floor(Math.random()*arr.length)];
}
});
Object.defineProperty(Array.prototype,"randomGets",{
configurable:true,
enumerable:false,
writable:true,
value:function(num){
if(num>this.length) num=this.length;
let arr=this.slice(0);
let list=[];
for(let i=0;i<num;i++){
list.push(arr.splice(Math.floor(Math.random()*arr.length),1)[0]);
}
return list;
}
});
Object.defineProperty(Array.prototype,"randomRemove",{
configurable:true,
enumerable:false,
writable:true,
value:function(num){
if(typeof num=='number'){
var list=[];
for(var i=0;i<num;i++){
if(this.length){
let list=[];
for(let i=0;i<num;i++){
if(!this.length) break;
list.push(this.randomRemove());
}
else{
break;
}
}
return list;
}
else{
return this.splice(Math.floor(Math.random()*this.length),1)[0];
}
}
});
Object.defineProperty(Array.prototype,"randomSort",{
configurable:true,
enumerable:false,
writable:true,
value:function(){
var list=[];
let list=[];
while(this.length){
list.push(this.randomRemove());
}
for(var i=0;i<list.length;i++){
for(let i=0;i<list.length;i++){
this.push(list[i]);
}
return this;
}
});
Object.defineProperty(Array.prototype, "randomGets", {
configurable:true,
enumerable:false,
writable:true,
value:function(num){
if(num>this.length){
num=this.length;
}
var arr=this.slice(0);
var list=[];
for(var i=0;i<num;i++){
list.push(arr.splice(Math.floor(Math.random()*arr.length),1)[0]);
}
return list;
}
});
Object.defineProperty(Array.prototype,"sortBySeat",{
configurable:true,
enumerable:false,
@ -8571,6 +8598,31 @@
return this;
}
});
/**
*@description 从数组中寻找某个特征最大的且通过筛选的第一个元素
*/
Object.defineProperty(Array.prototype,"maxBy",{
configurable:true,
enumerable:false,
writable:true,
value:function(sortBy,filter){
let list=this.filter(filter||(()=>true));
if(sortBy&&typeof sortBy=='function') list.sort((a,b)=>sortBy(a)-sortBy(b));
else list.sort();
return list[list.length-1];
}
});
Object.defineProperty(Array.prototype,"minBy",{
configurable:true,
enumerable:false,
writable:true,
value:function(sortBy,filter){
let list=this.filter(filter||(()=>true));
if(sortBy&&typeof sortBy=='function') list.sort((a,b)=>sortBy(a)-sortBy(b));
else list.sort();
return list[0];
}
});
//!!!WARNING!!!
//Will be deprecated in next verision
Object.defineProperty(Object.prototype,'hasNature',{