array.prototype

This commit is contained in:
kuangshen04 2023-10-07 23:10:54 +08:00
parent d8ffa36703
commit e0f9e919c7
1 changed files with 153 additions and 103 deletions

View File

@ -8418,159 +8418,209 @@
});
/*Map prototype end*/
Object.defineProperty(Array.prototype, "filterInD", {
configurable:true,
enumerable:false,
writable:true,
value:function(pos){
if(typeof pos!='string') pos='o';
return this.filter(card=>pos.includes(get.position(card,true)));
configurable: true,
enumerable: false,
writable: true,
value: function (pos) {
if (typeof pos != 'string') pos = 'o';
return this.filter(card => pos.includes(get.position(card, true)));
}
});
Object.defineProperty(Array.prototype, "someInD", {
configurable:true,
enumerable:false,
writable:true,
value:function(pos){
if(typeof pos!='string') pos='o';
return this.some(card=>pos.includes(get.position(card,true)));
configurable: true,
enumerable: false,
writable: true,
value: function (pos) {
if (typeof pos != 'string') pos = 'o';
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.
*/
Object.defineProperty(Array.prototype, "contains", {
configurable:true,
enumerable:false,
writable:true,
value:Array.prototype.includes
configurable: true,
enumerable: false,
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]);
configurable: true,
enumerable: false,
writable: true,
value: function () {
for (const arg of arguments) {
if (this.contains(arg)) continue;
this.push(arg);
}
return this;
}
});
Object.defineProperty(Array.prototype, "addArray", {
configurable:true,
enumerable:false,
writable:true,
value:function(arr){
for(var i=0;i<arr.length;i++){
this.add(arr[i]);
}
return this;
configurable: true,
enumerable: false,
writable: true,
value: function () {
return Array.prototype.add.apply(this,Array.from(arguments).flat());
}
});
Object.defineProperty(Array.prototype, "remove", {
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;
configurable: true,
enumerable: false,
writable: true,
value: function () {
for(const item of arguments){
const pos = this.indexOf(item);
if (pos == -1) continue;
this.splice(pos, 1);
}
this.splice(pos,1);
return this;
}
});
Object.defineProperty(Array.prototype, "removeArray", {
configurable:true,
enumerable:false,
writable:true,
value:function(arr){
for(var i=0;i<arr.length;i++){
this.remove(arr[i]);
}
configurable: true,
enumerable: false,
writable: true,
value: function () {
return Array.prototype.remove.apply(this,Array.from(arguments).flat());
}
});
Object.defineProperty(Array.prototype, "unique", {
configurable: true,
enumerable: false,
writable: true,
value: function () {
let uniqueArray = this.toUniqued();
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]);
return arr[Math.floor(Math.random()*arr.length)];
configurable: true,
enumerable: false,
writable: true,
value: function () {
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){
list.push(this.randomRemove());
}
else{
break;
}
configurable: true,
enumerable: false,
writable: true,
value: function (num) {
if (typeof num == 'number') {
let list = [];
for (let i = 0; i < num; i++) {
if (!this.length) break;
list.push(this.randomRemove());
}
return list;
}
else{
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", {
configurable:true,
enumerable:false,
writable:true,
value:function(){
var list=[];
while(this.length){
configurable: true,
enumerable: false,
writable: true,
value: function () {
var list = [];
while (this.length) {
list.push(this.randomRemove());
}
for(var i=0;i<list.length;i++){
for (var 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,
writable:true,
value:function(target){
lib.tempSortSeat=target;
configurable: true,
enumerable: false,
writable: true,
value: function (target) {
lib.tempSortSeat = target;
this.sort(lib.sort.seat);
delete lib.tempSortSeat;
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',{