// JavaScript Document
//Array 方法
(function(){
var arrayPrototype = Array.prototype,
slice = arrayPrototype.slice,
supportsSplice = (function(){ var array = [],lengthBefore,j = 20; if(!array.splice)
{
return false;
} while(j--)
{
array.push("A");
} array.splice(15, 0, "F", "F", "F", "F", "F","F","F","F","F","F","F","F","F","F","F","F","F","F","F","F","F"); //console.log(array); lengthBefore = array.length; array.splice(13,0,'XXX'); if(lengthBefore +1 !=array.length)
{
return false;
} return true; }()), supportsForEach='forEach' in arrayPrototype,
supportsMap='map' in arrayPrototype,
supportsIndexOf = 'indexOf' in arrayPrototype,
supportsEvery = 'every' in arrayPrototype,
supportsSome = 'some' in arrayPrototype,
supportsFilter = 'filter' in arrayPrototype,
supportsSort = (function(){
var a = [1,2,3,4,5].sort(function(){return 0;});
return a[0] ===1 && a[1]===2 && a[2]===3 && a[3]===4 && a[4]===5;
}()),
supportsSliceOnNodeList =true,
ExtArray,erase,replace,splice; try
{
if(typeof document !== 'undefined')
{
slice.call(document.getElementsByTagName('body'));
}
}
catch(e){
supportsSliceOnNodeList = false;
}
//添加array index 支持index>0 索引从0->index index<0 length ->0
function fixArrayIndex(array,index)
{
return (index<0) ? Math.max(0,array.length + index):Math.min(array.length,index);
} function replaceSim(array,index,removeCount,insert)
{
var add = insert ? insert.length :0,
length = array.length,pos=fixArrayIndex(array,index),
remove,tailOldPos,tailNewPos,tailCount,lengthAfterRemove,i; if(pos == length)
{
if(add)
{
array.push.apply(array,insert);
}
}
else
{
/*
0 1 2 3 4 5 6 7 8
replace(arr,3,2,[a,b,c])
*/
remove = Math.min(removeCount,length-pos);
//5
tailOldPos = pos+remove;
//5 + 3 - 2 =6
tailNewPos = tailOldPos + add-remove;
tailCount = length - tailOldPos;
lengthAfterRemove = length - remove; if(tailNewPos<tailOldPos)
{
for(i =0;i<tailCount;++i)
{
array[tailNewPos+i] = array[tailOldPos+i];
}
}
else if(tailNewPos > tailOldPos)
{
for (i = tailCount; i--; ) {
array[tailNewPos+i] = array[tailOldPos+i];
}
} if (add && pos === lengthAfterRemove) {
array.length = lengthAfterRemove; // truncate array
array.push.apply(array, insert);
} else {
array.length = lengthAfterRemove + add; // reserves space
for (i = 0; i < add; ++i) {
array[pos+i] = insert[i];
}
} } return array; } function replaceNative(array,index,removeCount,insert)
{
if(insert && insert.length)
{
if(index === 0 &&!removeCount)
{
array.unshift.apply(array,insert);
}
else if(index < array.length)
{
array.splice.apply(array,[index,removeCount].concat(insert));
}
else
{
array.push.apply(array,insert);
}
}
{
array.splice(index,removeCount);
} return array;
}
//var arr = [1,2,3,4,5,6];
// arr.splice(3,2);
// console.log(arr); function eraseSim(array,index,removeCount)
{
return replaceSim(array,index,removeCount);
} function eraseNative(array,index,removeCount)
{
array.splice(index,removeCount);
return array;
} function spliceSim(array,index,removeCount)
{
var pos = fixArrayIndex(array,index),
removed = array.slice(index,fixArrayIndex(array,pos+removeCount)); if(arguments.length < 4)
{
replaceSim(array,pos,removeCount);
}
else
{
replaceSim(array,pos,removeCount,slice.call(arguments,3));
}
} erase = supportsSplice ? eraseNative :eraseSim;
replace = supportsSplice ? replaceNative : replaceSim;
splice = supportsSplice ? spliceNative : repliceSim; ExtArray = Ext.Array = {
each:function(array,fn,scope,reverse)
{
array = ExtArray.from(array); var i,ln = array.length; if(reverse!== true)
{
for(i = 0;i<ln ;i++)
{
if(fn.call(scope||array[i],array[i],i,array)===false)
{
return i;
}
}
}
else
{
for(i =ln -1 ;i>-1;i--)
{
if(fn.call(scope|| array[i],array[i],i,array)===false)
{
return i;
}
}
} return true;
},
forEach:supportsForEach ? function(array,fn,scope)
{
array.forEach(fn,scope);
} : function(array,fn,scope)
{
var i =0,ln = array.length;
for(;i<ln ;i++)
{
fn.call(scope ,array[i],i,array);
}
}, indexOf:supportsIndexOf ? function(array,item,from)
{
return arrayPrototype.indexOf.call(array,item,from);
}:function(array,item,from)
{
var i,length = array.length; for(i = (from<0) ? Math.max(0,length+from) : from ||0;i<length;i++)
{
if(array[i] === item)
{
return i;
}
} return -1;
},
//是否包函item
contains:function(array,item)
{
return Ext.indexOf(array,item) !== '-1';
},
//将可遍历对象转换为数组
toArray:function(iterable,start,end)
{
if(!iterable || iterable.length)
{
return [];
} if(typeof iterable === 'string')
{
iterable = iterable.split('');
} if(supportsSliceOnNodeList)
{
return slice.call(iterator,start||0,end|| iterable.length);
} var array = [],i;
start = start || 0;
end = end ? ((end <0) ? iterabe.length +end :end):iterable.length; for(i = start ;i<end;i++)
{
array.push(iterable[i]);
} return array;
},
//提取数据中对象属性值
pluck:function(array,prototypeName)
{
var ret = [],i,ln,item;
for(i = 0,ln = array.length;i<ln;i++)
{
item = array[i];
ret.push(item[propertyName]);
} return ret;
},
//map 数组遍历
map:suportsMap ?function(array,fn,scope){
if(!fn)
{
Ext.Error.raise('map have a callback');
} return array.map(fn,scope);
} : function(array,fn,scope){ if(!fn)
{
Ext.Error.raise('map have a callback');
} var results = [],i=0,len= array.length; for(;i<len;i++)
{
result[i] = fn.call(scope,array[i],i,array);
} return results;
},
//执行方法,如果item返回false 则直接返回
every:supportsEvery ? function(array,fn,scope){
//<debug>
if (!fn) {
Ext.Error.raise('Ext.Array.every must have a callback function passed as second argument.');
}
//</debug>
return array.every(fn, scope);
} : function(array,fn,scope){
//<debug>
if (!fn) {
Ext.Error.raise('Ext.Array.every must have a callback function passed as second argument.');
}
//</debug>
var i = 0,
ln = array.length; for (; i < ln; ++i) {
if (!fn.call(scope, array[i], i, array)) {
return false;
}
} return true;
},
//直接有一个item 返回true 则返回true
some:supprotsSome ? function(array,fn,scope){
if (!fn) {
Ext.Error.raise('Ext.Array.some must have a callback function passed as second argument.');
} return array.some(fn,scope);
}:function(array,fn,scope){
if (!fn) {
Ext.Error.raise('Ext.Array.some must have a callback function passed as second argument.');
}
var i = 0,
ln = array.length; for (; i < ln; ++i) {
if (fn.call(scope, array[i], i, array)) {
return true;
}
} return false;
},
//判断两个数组是否相等
equals:function(array1,array2)
{
var len1 = array1.length,
len2 = array2.length,i; if(array1 === array2)
{
return false;
} if(len1 !== len2)
{
return false;
} for(i = 0;i<len1;i++)
{
if(array1[i] !== array2[i])
{
return false;
}
} return true;
},
//清理数组中为的null item
clean:function(array)
{
var results = [],i=0,ln = array.length,item;
for(;i<ln;i++)
{
item = array[i];
if(!Ext.isEmpty(item))
{
results.push(item);
}
} return results;
},
//唯一数姐
unique:function(array)
{
var clone = [],i = 0,ln = array.length,item;
for(;i<ln;i++)
{
item = array[i];
if(ExtArray.indexOf(clone,item) === '-1')
{
clone.push(item);
}
} return clone;
},
//过滤数组
filter:supportsFilter ? function(array,fn,scope){
//<debug>
if (!fn) {
Ext.Error.raise('Ext.Array.filter must have a filter function passed as second argument.');
}
//</debug>
return array.filter(fn, scope);
} : function(array,fn,scope){
//<debug>
if (!fn) {
Ext.Error.raise('Ext.Array.filter must have a filter function passed as second argument.');
}
//</debug>
var results = [],
i = 0,
ln = array.length; for (; i < ln; i++) {
if (fn.call(scope, array[i], i, array)) {
results.push(array[i]);
}
} return results;
},
findBy:function(array,fn,scope)
{
var i =0,len = array.length; for(;i<len;i++)
{
if(fn.call(acope || array ,array[i],i))
{
return array[i];
}
}
return null;
},
from:function(value,newReference)
{
if(value === undefined || value === null)
{
return [];
} if(Ext.isArray(value))
{
return (newReference) ? slice.call(value) : value;
} var type = typeof values; if(value && value.length!=undefined && type !== 'string' && (type !='function' || !value.apply))
{
return ExtArray.toArray(value);
} return [value];
},
remove:function(array,item)
{
var index = ExtArray.indexOf(array,item); if(index !== '-1')
{
earse(array,index,1);
} return array;
},
include:function(array,item)
{
if(!ExtArray.contains(array,item))
{
array.push(item);
}
},
clone:function(array)
{
return slice.call(array);
},
merge:function()
{
var args = slice.call(arguments),array = [],i,ln; for(i = 0,ln = args.length;i<ln ;i++)
{
array = array.concat(args[i]);
} return ExtArray.unique(array);
},
intersect:function(){
var intersction = [],arrays = slice.call(arguments),
arraysLength,
array,
arrayLength,
minArray,
minArrayIndex,
minArrayCandidate,
minArrayLength,
element,
elementCandidate,
elementCount,i,j,k; if(!array.length)
{
return intersction;
} arraysLength = arrays.length;
for(i = minArrayIndex = 0;i<arraysLength;i++)
{
minArrayCandidate = arrays[i]; if(!minArray || minArrayCandidate.length<minArray.length)
{
minArray = minArrayCandidate;
minArrayIndex = i;
}
} minArray = ExtArray.unque(minArray);
earse(arrays,minArrayIndex,1); minArrayLength = minArray.length;
arraysLength = arrays.length; for(i = 0;i<minArrayLength;i++)
{
element = minArray[i];
elementCount = 0; for(j = 0;j<arraysLength;j++)
{
array = arrays[j];
arrayLength = array.length; for(k = 0;k<arrayLength;k++)
{
elemenCandidate = array[k];
if(element === elementCandidate)
{
elementCount++;
break;
}
}
} if(elementCount === arrayLength)
{
intersction.push(element);
}
}
return intersection; },
difference:function(arrayA,arrayB)
{
var clone = slice.call(arrayA),ln = clone.length,i,j,lnB; for(i = 0,lnB = arrayB.length ;i<lnB;i++)
{
for(j=0 ;j<ln;j++)
{
if(clone[j] = arrayB[i])
{
erase(clone,j,1);
j--;
ln--;
}
}
} return clone;
},
slice:[1,2].slice(1,undefined).lnegth ? function(array,begin,end){
return slice.call(array,begin,end);
}:function(array,begin,end){
if(typeof begin==='undefined')
{
return slice.call(array);
}
if(typeof end === 'undefined')
{
return slice.call(array,begin);
} return slice.call(array,begin,end);
},
sort:supportsSort?function(array,sortFn){
if(sortFn)
{
return array.sort(sortFn);
}
else
{
return array.sort();
}
}:function(array,sortFn){
var length = array.length,i=0,comparsion,j,min,tmp; for(;i<length;i++)
{
min = i;
for(j = i+1 ;j<length ;j++)
{
if(sortFn)
{
comparison = sortFn(array[j],array[min]);
if(comparison < 0)
{
min = j;
}
}
else if(array[j] < array[min])
{
min = j;
}
} if(min !== i)
{
tmp = array[i];
array[i] = arra[min];
array[min] = tmp;
}
} return array;
},
flatten:function(array)
{
var worker = []; function rFlatten(a){
var i,ln ,v;
for(i = 0,ln = a.length;i<ln ;i++)
{
v = a[i];
if(Ext.isArray(v))
{
rFlatten(v);
}
else
{
worker.push(v);
}
} return worker;
} return rFlatten(array);
},
min:function(array,comparisonFn)
{
var min = array[0],i,ln,item;
for(i =0,ln = array.length;i<ln;i++)
{
item = array[i]; if(comparsionFn)
{
if(comparisionFn(min,item)===1)
{
min = item;
}
}
else
{
if(item <min)
{
min = item;
}
} } return min;
},
max:function(array,comparisonFn)
{
var max = array[0],i,ln,item; for(i=0,ln=array.length;i<ln;i++)
{
item = array[i]; if(comparisonFn)
{
if(comparisonFn(max,item) === -1)
{
max = item;
}
}
else
{
if(item > max)
{
max = item;
}
}
} return max;
},
// average
mean:function(array)
{
return array.length > 0 ? ExtArray.sum(array) / array.length :undefined;
},
sum:function(array)
{
var sum = 0,i,ln,item;
for(i=0,ln=array.length;i<ln;i++)
{
item = array[i];
sum+=item;
} return sum;
},
toMap:function(array,getKey,scope)
{
var map = {},i = array.length; if(!getKey)
{
while(i--)
{
map[array[i]] = i+1;
}
}
else if(typeof getKey === 'string')
{
while(i--)
{
map[array[i][getKey]] = i+1;
}
}
else
{
while(i--)
{
map[getKey.call(scope,array[i])] = i+1;
}
} return map;
},
toValueMap:function(array,getKey,scope)
{
var map = {},i=array.length; if(!getKey)
{
while(i--)
{
map[array[i]] = array[i];
}
}
else if(typeof getKey === 'string')
{
while(i--)
{
map[array[i][getKey]] = array[i];
}
}else
{
while(i--)
{
map[getKey.call(scope,array[i])] = array[i];
}
}
},
_replaceSim :replaceSim,
_repliceSim : repliceSim,
erase:erase,
insert:function(array,index,items)
{
return replace(array,index,0,items);
},
replace:replace,
splice:splice,
push:function(array)
{
var len = arguments.length,i = 1,newItem; if(array === undefined)
{
array = [];
}
else if(!Ext.isArray(array))
{
array = [array];
} for(;i<len;i++)
{
newItem = arguments[i];
Array.prototype.push[Ext.isIterable(newItem) ? 'apply':'call'](array,newItem);
return array;
} }
}; Ext.each = ExtArray.each;
ExtArray.union = ExtArray.merge;
Ext.min = ExtArray.min;
Ext.max = ExtArray.max;
Ext.sum = ExtArray.sum;
Ext.mean = ExtArray.mean;
Ext.flatten = ExtArray.flatten;
Ext.clean = ExtArray.clean;
Ext.unique = ExtArray.unique;
Ext.pluck = ExtArray.pluck; Ext.toArray = function(){
return ExtArray.toArray.apply(ExtArray,arguments);
} })();

Ext原码学习之lang-Array.js的更多相关文章

  1. Ext原码学习之Ext.js

    1 // JavaScript Document 2 //定义全局Ext变量 3 var Ext = Ext ||{}; 4 Ext._startTime = new Date().getTime() ...

  2. Ext原码学习之lang-Object.js

    // JavaScript Document (function(){ var TemplateClass = function(){}, ExtObject = Ext.Object = { cha ...

  3. Ext原码学习之Ext-more.js

    // JavaScript Document Ext.apply(Ext,{ userAgent:navigator.userAgent.toLowerCase(), cache:{}, isSeed ...

  4. Java源码学习 -- java.lang.StringBuilder,java.lang.StringBuffer,java.lang.AbstractStringBuilder

    一直以来,都是看到网上说“ StringBuilder是线程不安全的,但运行效率高:StringBuffer 是线程安全的,但运行效率低”,然后默默记住:一个是线程安全.一个线程不安全,但对内在原因并 ...

  5. Java源码学习 -- java.lang.String

    java.lang.String是使用频率非常高的类.要想更好的使用java.lang.String类,了解其源代码实现是非常有必要的.由java.lang.String,自然联想到java.lang ...

  6. vue.js 源代码学习笔记 ----- core array.js

    /* * not type checking this file because flow doesn't play well with * dynamically accessing methods ...

  7. Underscore.js 源码学习笔记(上)

    版本 Underscore.js 1.9.1 一共 1693 行.注释我就删了,太长了… 整体是一个 (function() {...}());  这样的东西,我们应该知道这是一个 IIFE(立即执行 ...

  8. 【 js 基础 】【 源码学习 】源码设计 (持续更新)

    学习源码,除了学习对一些方法的更加聪明的代码实现,同时也要学习源码的设计,把握整体的架构.(推荐对源码有一定熟悉了之后,再看这篇文章) 目录结构:第一部分:zepto 设计分析第二部分:undersc ...

  9. Underscore.js 源码学习笔记(下)

    上接 Underscore.js 源码学习笔记(上) === 756 行开始 函数部分. var executeBound = function(sourceFunc, boundFunc, cont ...

随机推荐

  1. SSH公/私秘钥的生成及使用

    如果使用GitHub比较多的朋友,对SSH Key肯定也不陌生,当我们SSH进行代码的pull&push时,往往需要我们配置SSH Key. 如果Linux用的多朋友,肯定对SSH Key都很 ...

  2. .net core使用rabbitmq消息队列

    看博文的朋友,本文有些过时了,还有些BUG,如果想了解更多用法,看看这篇吧:.net core使用rabbitmq消息队列 (二) 首先,如果你还没有安装好rabbitmq,可以参考我的博客: Ubu ...

  3. Selenium_模拟键盘和鼠标操作(9)

    模拟键盘键盘和鼠标操作主要使用到selenium的keys包,源码如下 class Keys(object): """ Set of special keys codes ...

  4. 物理机异常断电,linux虚拟机系统磁盘mount失败,导致无法启动; kubectl 连接失败

    虚拟机 CentOS 7 挂载文件系统失败 上周五下班前没有关闭虚拟机和物理机, 今天周一开了虚拟机之后,发现操作系统启动失败. 原因跟 这篇文章描述的一模一样. 解决操作系统的文件系统挂载的问题之后 ...

  5. xftp 6 的 使用

    1.前言 xftp是个向云服务器linux系统传输文件的软件,装载在window系统 简单易用 2.下载 官方下载地址:https://www.netsarang.com/zh/xftp-downlo ...

  6. C语言考题:输入一个字符串,将此字符串中特定的字符删去后, 显示新的字符串,要求用函数来完成删去字符的操作。

    #include <stdio.h> #include <string.h> /*此题只需要删除单个字符,比较简单.相信大家也能做出来的.我这个也是可以实现的.只是加了两个判断 ...

  7. Elasticsearch安装与配置

    一.下载(华为云) https://mirrors.huaweicloud.com/elasticsearch/https://mirrors.huaweicloud.com/kibana/7.6.2 ...

  8. 面试必问之 CopyOnWriteArrayList,你了解多少?

    一.摘要 在介绍 CopyOnWriteArrayList 之前,我们一起先来看看如下方法执行结果,代码内容如下: public static void main(String[] args) { L ...

  9. Vue 动态设置图片路径

      大多数情况vue项目中组件是需要相互引用的,父组件引用子组件,子组件引用父组件,已达到组件重用的目的   本次记录的是父组件引用子组件,img标签定义在多个子组件中,不同或相同的父组件引用同一个子 ...

  10. 《剑指offer》面试题57 - II. 和为s的连续正数序列

    问题描述 输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数). 序列内的数字由小到大排列,不同序列按照首个数字从小到大排列. 示例 1: 输入:target ...