Comparing version 0.2.6 to 0.2.7
{ | ||
"name": "rubyisms", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "Ruby style ES5 prototype extensions.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -22,3 +22,3 @@ <p align="center"> | ||
```JavaScript | ||
(5).times(function (n) { | ||
(5).times(function () { | ||
console.log('Yo.'); | ||
@@ -25,0 +25,0 @@ }); |
137
rubyisms.js
@@ -7,6 +7,23 @@ // Colin 'Oka' Hall-Coates | ||
'use strict'; | ||
var _bin = { | ||
regex: { | ||
symbolWithCombiningMarks: /([\0-\u02FF\u0370-\u1AAF\u1B00-\u1DBF\u1E00-\u20CF\u2100-\uD7FF\uE000-\uFE1F\uFE30-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])([\u0300-\u036F\u1AB0-\u1AFF\u1DC0-\u1DFF\u20D0-\u20FF\uFE20-\uFE2F]+)/g, | ||
surrogatePair: /([\uD800-\uDBFF])([\uDC00-\uDFFF])/g | ||
}, | ||
types: { | ||
arr : '[object Array]', | ||
bool : '[object Boolean]', | ||
func : '[object Function]', | ||
nul : '[object Null]', | ||
num : '[object Number]', | ||
obj : '[object Object]', | ||
str : '[object String]', | ||
und : '[object Undefined]' | ||
} | ||
}; | ||
function define(s, v) { | ||
var type = Object.prototype.toString.call(this), | ||
isObject = (type === '[object Object]'); | ||
isObject = (type === _bin.types.obj); | ||
@@ -68,13 +85,13 @@ delete this[s]; | ||
uniq: (function () { | ||
var arr = '[object Array]', | ||
bool = '[object Boolean]', | ||
func = '[object Function]', | ||
nul = '[object Null]', | ||
num = '[object Number]', | ||
obj = '[object Object]', | ||
str = '[object String]', | ||
und = '[object Undefined]'; | ||
var arr = _bin.types.arr, | ||
bool = _bin.types.bool, | ||
func = _bin.types.func, | ||
nul = _bin.types.nul, | ||
num = _bin.types.num, | ||
obj = _bin.types.obj, | ||
str = _bin.types.str, | ||
und = _bin.types.und; | ||
return (function () { | ||
var s = {}, b = {}, n = {}, u = {}, o = [], r = [], i, c, k, p, item, type; | ||
var s = {}, b = {}, n = {}, u = {}, o = [], r = [], i, c, p, item, type; | ||
@@ -92,5 +109,7 @@ for (i = 0; i < this.length; i++) { | ||
} else if (type === obj || type === func || type === arr) { | ||
objs: | ||
for (c = 0; c < o.length; c++) { | ||
if (o[c] === item) { | ||
p = false; | ||
break objs; | ||
} | ||
@@ -184,10 +203,10 @@ } | ||
array: function () { | ||
return Object.prototype.toString.call(this) === '[object Array]'; | ||
return Object.prototype.toString.call(this) === _bin.types.arr; | ||
}, | ||
bool: function () { | ||
return Object.prototype.toString.call(this) === '[object Boolean]'; | ||
return Object.prototype.toString.call(this) === _bin.types.bool; | ||
}, | ||
empty: function () { | ||
var type = Object.prototype.toString.call(this); | ||
if (type === '[object Object]') { | ||
if (type === _bin.types.obj) { | ||
return Object.keys(this).length < 1; | ||
@@ -197,21 +216,21 @@ } | ||
func: function () { | ||
return Object.prototype.toString.call(this) === '[object Function]'; | ||
return Object.prototype.toString.call(this) === _bin.types.func; | ||
}, | ||
numeric: function () { | ||
return Object.prototype.toString.call(this) === '[object Number]' && this === this; | ||
return Object.prototype.toString.call(this) === _bin.types.num && this === this; | ||
}, | ||
object: function () { | ||
return Object.prototype.toString.call(this) === '[object Object]'; | ||
return Object.prototype.toString.call(this) === _bin.types.obj; | ||
}, | ||
size: function () { | ||
var type = Object.prototype.toString.call(this); | ||
if (type === '[object Array]' || | ||
type === '[object String]' || | ||
type === '[object Function]') return this.length; | ||
else if (type === '[object Object]') return Object.keys(this).length; | ||
else if (type === '[object Boolean]') return (this ? 1 : 0); | ||
else if (type === '[object Number]' && this === this) return this; | ||
if (type === _bin.types.arr || | ||
type === _bin.types.str || | ||
type === _bin.types.func) return this.length; | ||
else if (type === _bin.types.obj) return Object.keys(this).length; | ||
else if (type === _bin.types.bool) return (this ? 1 : 0); | ||
else if (type === _bin.types.num && this === this) return this; | ||
}, | ||
string: function () { | ||
return Object.prototype.toString.call(this) === '[object String]'; | ||
return Object.prototype.toString.call(this) === _bin.types.str; | ||
}, | ||
@@ -224,3 +243,3 @@ type: function () { | ||
// Unset globals | ||
G[key] = undefined; | ||
delete G[key]; | ||
}); | ||
@@ -233,9 +252,33 @@ | ||
chars: function () { | ||
return this.split(''); | ||
var i, c, len = this.length, r = []; | ||
for (i = 0; i < len; i++) { | ||
c = this.substr(i, 2); | ||
if (_bin.regex.surrogatePair.test(c) || | ||
_bin.regex.symbolWithCombiningMarks.test(c)) { | ||
r.push(c); | ||
i++; | ||
} else { | ||
r.push(this.substr(i, 1)); | ||
} | ||
} | ||
return r; | ||
}, | ||
chop: function () { | ||
return this.slice(0, this.length - 1); | ||
var c = this.substr(this.length - 2, 2); | ||
if (_bin.regex.surrogatePair.test(c) || | ||
_bin.regex.symbolWithCombiningMarks.test(c)) { | ||
return this.substr(0, this.length - 2); | ||
} else { | ||
return this.substring(0, this.length - 1); | ||
} | ||
}, | ||
chr: function () { | ||
return this.substring(0, 1); | ||
var c = this.substring(0, 2); | ||
if (_bin.regex.surrogatePair.test(c) || | ||
_bin.regex.symbolWithCombiningMarks.test(c)) { | ||
return c; | ||
} else { | ||
return c.substring(0, 1); | ||
} | ||
}, | ||
@@ -250,16 +293,13 @@ downcase: function () { | ||
// Credit Mathias Bynens, github.com/mathiasbynens/esrever | ||
var cM = /([\0-\u02FF\u0370-\u1AAF\u1B00-\u1DBF\u1E00-\u20CF\u2100-\uD7FF\uE000-\uFE1F\uFE30-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])([\u0300-\u036F\u1AB0-\u1AFF\u1DC0-\u1DFF\u20D0-\u20FF\uFE20-\uFE2F]+)/g, | ||
sP = /([\uD800-\uDBFF])([\uDC00-\uDFFF])/g; | ||
function r (s) { | ||
s = s.replace(cM, function($0, $1, $2) { | ||
return r($2) + $1; | ||
}).replace(sP, '$2$1'); | ||
var r = '', i = s.length; | ||
while (i--) r += s.charAt(i); | ||
return r; | ||
function rev (s) { | ||
s = s.replace(_bin.regex.symbolWithCombiningMarks, function($0, $1, $2) { | ||
return rev($2) + $1; | ||
}).replace(_bin.regex.surrogatePair, '$2$1'); | ||
var o = '', i = s.length; | ||
while (i--) o += s.charAt(i); | ||
return o; | ||
} | ||
return (function () { | ||
return r(this); | ||
return rev(this); | ||
}); | ||
@@ -270,5 +310,4 @@ }()), | ||
for (i = 0; i < this.length; i++) { | ||
c = this[i].toUpperCase(); | ||
if (c === this[i]) c = this[i].toLowerCase(); | ||
s += c; | ||
c = this.charAt(i).toUpperCase(); | ||
s += (c === this.charAt(i) ? c.toLowerCase() : c); | ||
} | ||
@@ -285,3 +324,3 @@ return s; | ||
for (var i = 0; i < this.length; i++) { | ||
if (Object.prototype.toString.call(this[i]) !== '[object Object]') continue; | ||
if (Object.prototype.toString.call(this[i]) !== _bin.types.obj) continue; | ||
if (this[i].hasOwnProperty(key)) return this[i]; | ||
@@ -293,4 +332,4 @@ } | ||
if (args < 1) throw new Error('1 argument expected, got: ' + args); | ||
var c = 0, i = 0, t = ((typeof f === 'undefined' ? true : f) | ||
&& typeof vfn === 'function'); | ||
var c = 0, i = 0, t = ((typeof f === 'undefined' ? true : f) && | ||
typeof vfn === 'function'); | ||
for (i; i < this.length; i++) { | ||
@@ -386,3 +425,3 @@ if ((t && vfn.call(this, this[i], i)) || (!t && this[i] === vfn)) c++; | ||
var type = Object.prototype.toString.call(n); | ||
if (type !== '[object Number]' || n.toFixed() != n) { | ||
if (type !== _bin.types.num || n.toFixed() != n) { | ||
throw new TypeError('Integer value required'); | ||
@@ -408,3 +447,3 @@ } | ||
var type = Object.prototype.toString.call(n); | ||
if (type !== '[object Number]' || n.toFixed() != n) { | ||
if (type !== _bin.types.num || n.toFixed() != n) { | ||
throw new TypeError('Integer value required'); | ||
@@ -426,3 +465,3 @@ } | ||
var type = Object.prototype.toString.call(this); | ||
if (type !== '[object Object]') return; | ||
if (type !== _bin.types.obj) return; | ||
if (typeof fn !== 'function') throw new TypeError('Expected function'); | ||
@@ -442,3 +481,3 @@ for (var k in this) { | ||
args = Array.prototype.slice.call(arguments); | ||
if (type !== '[object Object]') return; | ||
if (type !== _bin.types.obj) return; | ||
if (args.length < 1) throw new Error('No value given'); | ||
@@ -448,3 +487,3 @@ | ||
return (typeof r === 'function' ? r.call(this, v) : r) | ||
return (typeof r === 'function' ? r.call(this, v) : r); | ||
} | ||
@@ -451,0 +490,0 @@ }); |
@@ -11,2 +11,4 @@ var chai = require('chai'), | ||
var oddity = '𝌆 bar mañana mañana'; | ||
describe('#capitalize', function () { | ||
@@ -33,2 +35,6 @@ var foo = 'hello', | ||
it('should work on non standard characters', function () { | ||
expect(oddity.chars.join('')).to.equal(oddity); | ||
}); | ||
it('should not affect the original', function () { | ||
@@ -61,2 +67,6 @@ foo.should.equal('hello'); | ||
it('should work on non standard characters', function () { | ||
expect(oddity.chr).to.eql('𝌆'); | ||
}); | ||
it('should not affect the original', function () { | ||
@@ -105,2 +115,6 @@ foo.should.equal('hello'); | ||
it('should work on non standard characters', function () { | ||
expect(oddity.reverse.reverse).to.equal(oddity); | ||
}); | ||
it('should not affect the original', function () { | ||
@@ -107,0 +121,0 @@ foo.should.equal('hello'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
274961
1302