Skip to content

Commit

Permalink
remove map , and enhance filter, forEach polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
leeluolee committed Nov 20, 2015
1 parent 9a43028 commit 8a5ab18
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 261 deletions.
77 changes: 62 additions & 15 deletions dist/regular.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ return /******/ (function(modules) { // webpackBootstrap




/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
Expand Down Expand Up @@ -1097,6 +1098,9 @@ return /******/ (function(modules) { // webpackBootstrap
/***/ function(module, exports, __webpack_require__) {

/* WEBPACK VAR INJECTION */(function(global) {__webpack_require__(19)();



var _ = module.exports;
var entities = __webpack_require__(20);
var slice = [].slice;
Expand Down Expand Up @@ -1488,11 +1492,20 @@ return /******/ (function(modules) { // webpackBootstrap
}


_.map= function(array, callback){
var res = [];
for (var i = 0, len = array.length; i < len; i++) {
res.push(callback(array[i], i));
}
return res;
}

_.log = function(msg, type){
function log(msg, type){
if(typeof console !== "undefined") console[type || "log"](msg);
}

_.log = log;




Expand All @@ -1516,6 +1529,9 @@ return /******/ (function(modules) { // webpackBootstrap
}






/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }())))

Expand Down Expand Up @@ -4238,6 +4254,7 @@ return /******/ (function(modules) { // webpackBootstrap
return o2;
}


module.exports = function(){
// String proto ;
extend(String.prototype, {
Expand All @@ -4256,24 +4273,54 @@ return /******/ (function(modules) { // webpackBootstrap
}
return -1;
},
forEach: function(callback, context){
for (var i = 0, len = this.length; i < len; i++) {
callback.call(context, this[i], i, this);
// polyfill from MDN
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
forEach: function(callback, ctx){
var k = 0;

// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
var O = Object(this);

var len = O.length >>> 0;

if ( typeof callback !== "function" ) {
throw new TypeError( callback + " is not a function" );
}
},
filter: function(callback, context){
var res = [];
for (var i = 0, length = this.length; i < length; i++) {
var pass = callback.call(context, this[i], i, this);
if(pass) res.push(this[i]);

// 7. Repeat, while k < len
while( k < len ) {

var kValue;

if ( k in O ) {

kValue = O[ k ];

callback.call( ctx, kValue, k, O );
}
k++;
}
return res;
},
map: function(callback, context){
// @deprecated
// will be removed at 0.5.0
filter: function(fun, context){

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();

var res = [];
for (var i = 0, length = this.length; i < length; i++) {
res.push(callback.call(context, this[i], i, this));
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i];
if (fun.call(context, val, i, t))
res.push(val);
}
}

return res;
}
});
Expand Down Expand Up @@ -4714,7 +4761,7 @@ return /******/ (function(modules) { // webpackBootstrap
if(mode === 2){ // auto removed
dom.addClass( node, className );

activeClassName = className.split(/\s+/).map(function(name){
activeClassName = _.map(className.split(/\s+/), function(name){
return name + '-active';
}).join(" ");

Expand Down
4 changes: 2 additions & 2 deletions dist/regular.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/helper/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ animate.startClassAnimate = function ( node, className, callback, mode ){
if(mode === 2){ // auto removed
dom.addClass( node, className );

activeClassName = className.split(/\s+/).map(function(name){
activeClassName = _.map(className.split(/\s+/), function(name){
return name + '-active';
}).join(" ");

Expand Down
57 changes: 44 additions & 13 deletions src/helper/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function extend(o1, o2 ){
return o2;
}


module.exports = function(){
// String proto ;
extend(String.prototype, {
Expand All @@ -27,24 +28,54 @@ module.exports = function(){
}
return -1;
},
forEach: function(callback, context){
for (var i = 0, len = this.length; i < len; i++) {
callback.call(context, this[i], i, this);
// polyfill from MDN
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
forEach: function(callback, ctx){
var k = 0;

// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
var O = Object(this);

var len = O.length >>> 0;

if ( typeof callback !== "function" ) {
throw new TypeError( callback + " is not a function" );
}
},
filter: function(callback, context){
var res = [];
for (var i = 0, length = this.length; i < length; i++) {
var pass = callback.call(context, this[i], i, this);
if(pass) res.push(this[i]);

// 7. Repeat, while k < len
while( k < len ) {

var kValue;

if ( k in O ) {

kValue = O[ k ];

callback.call( ctx, kValue, k, O );
}
k++;
}
return res;
},
map: function(callback, context){
// @deprecated
// will be removed at 0.5.0
filter: function(fun, context){

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();

var res = [];
for (var i = 0, length = this.length; i < length; i++) {
res.push(callback.call(context, this[i], i, this));
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i];
if (fun.call(context, val, i, t))
res.push(val);
}
}

return res;
}
});
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Regular.parse = function(str, options){
return !options.stringify? ast : JSON.stringify(ast);
}


17 changes: 16 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require('./helper/shim.js')();



var _ = module.exports;
var entities = require('./helper/entities.js');
var slice = [].slice;
Expand Down Expand Up @@ -390,11 +393,20 @@ _.fixObjStr = function(str){
}


_.map= function(array, callback){
var res = [];
for (var i = 0, len = array.length; i < len; i++) {
res.push(callback(array[i], i));
}
return res;
}

_.log = function(msg, type){
function log(msg, type){
if(typeof console !== "undefined") console[type || "log"](msg);
}

_.log = log;




Expand All @@ -418,3 +430,6 @@ _.getCompileFn = function(source, ctx, options){
}





Loading

0 comments on commit 8a5ab18

Please sign in to comment.