New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rubyisms

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rubyisms - npm Package Compare versions

Comparing version 0.2.7 to 0.3.0

0

docs/arrays/README.md

@@ -0,0 +0,0 @@ # Arrays

@@ -0,0 +0,0 @@ # Booleans

@@ -0,0 +0,0 @@ # Functions

@@ -0,0 +0,0 @@ # Numbers

@@ -0,0 +0,0 @@ # Objects

@@ -0,0 +0,0 @@ # Documentation

@@ -0,0 +0,0 @@ # Strings

2

package.json
{
"name": "rubyisms",
"version": "0.2.7",
"version": "0.3.0",
"description": "Ruby style ES5 prototype extensions.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -21,3 +21,3 @@ <p align="center">

```JavaScript
```javascript
(5).times(function () {

@@ -36,7 +36,9 @@ console.log('Yo.');

$ npm install rubyisms
```shell
$ npm install rubyisms
```
## Documentation
Get to know the methods, [here](https://github.com/Oka-/rubyisms/blob/master/docs/README.md).
Get to know the methods, [here](https://github.com/Okahyphen/rubyisms/blob/master/docs/README.md).

@@ -47,3 +49,3 @@ [npm-url]: https://www.npmjs.com/package/rubyisms

[travis-url]: https://travis-ci.org/Oka-/rubyisms
[travis-build]: https://travis-ci.org/Oka-/rubyisms.svg?branch=master
[travis-url]: https://travis-ci.org/Okahyphen/rubyisms
[travis-build]: https://travis-ci.org/Okahyphen/rubyisms.svg?branch=master

@@ -8,24 +8,24 @@ // Colin 'Oka' Hall-Coates

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]'
}
var toString = Object.prototype.toString;
var 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 === _bin.types.obj);
delete this[s];
var 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
};
/* jshint -W040 */
function define (s, v) {
var isObject = (toString.call(this) === types.obj);
Object.defineProperty(this, s, {

@@ -38,10 +38,11 @@ value: v,

}
/* jshint +W040 */
function strap(g, s) {
return {get: g, set: function (v) {
function strap (g, s) {
return { get: g, set: function (v) {
define.call(this, s, v);
}, configurable: true};
}, configurable: true };
}
function minor(prototype, o, callback) {
function minor (prototype, o, callback) {
for (var k in o) {

@@ -55,3 +56,3 @@ if (o.hasOwnProperty(k) && !prototype.hasOwnProperty(k)) {

function major(prototype, o, callback) {
function major (prototype, o, callback) {
for (var k in o) {

@@ -71,10 +72,17 @@ if (o.hasOwnProperty(k) && !prototype.hasOwnProperty(k)) {

clear: function () {
while (this.length > 0) this.pop(); return this;
while (this.length > 0) this.pop();
return this;
},
compact: function () {
var o = [], i;
for (i = 0; i < this.length; i++) {
if(typeof this[i] !== 'undefined') o.push(this[i]);
var output = [],
length = this.length,
index = 0,
current;
for (; index < length; index++) {
current = this[index];
if (typeof current !== 'undefined') output.push(current);
}
return o;
return output;
},

@@ -88,17 +96,21 @@ empty: function () {

uniq: (function () {
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;
var arr = types.arr,
bool = types.bool,
func = types.func,
nul = types.nul,
num = types.num,
obj = types.obj,
str = types.str,
und = types.und;
return (function () {
var s = {}, b = {}, n = {}, u = {}, o = [], r = [], i, c, p, item, type;
for (i = 0; i < this.length; i++) {
item = this[i];
type = Object.prototype.toString.call(item);
var s = {}, b = {}, n = {}, u = {}, o = [], r = [], p, c,
item, type, length, index;
length = this.length;
index = 0;
for (; index < length; index++) {
item = this[index];
type = toString.call(item);
p = true;

@@ -127,3 +139,3 @@ if (type === str && !s.hasOwnProperty(item)) {

}
return r;

@@ -172,5 +184,4 @@ });

next: function () {
if (this.toFixed() != this || this === Infinity) {
if (this.toFixed() != this || this === Infinity)
throw new TypeError('Self must be integer');
}

@@ -180,3 +191,3 @@ return this + 1;

nonzero: function () {
return (this !== 0 ? this: null);
return this !== 0 ? this : null;
},

@@ -187,10 +198,9 @@ odd: function () {

polar: function () {
return (isFinite(this) &&
[Math.abs(this), (this < 0 ? Math.PI : 0)]) ||
undefined;
return (isFinite(this) &&
[Math.abs(this), (this < 0 ? Math.PI : 0)]) ||
undefined;
},
pred: function () {
if (this.toFixed() != this || this === Infinity) {
if (this.toFixed() != this || this === Infinity)
throw new TypeError('Self must be integer');
}

@@ -209,10 +219,9 @@ return this - 1;

array: function () {
return Object.prototype.toString.call(this) === _bin.types.arr;
return toString.call(this) === types.arr;
},
bool: function () {
return Object.prototype.toString.call(this) === _bin.types.bool;
return toString.call(this) === types.bool;
},
empty: function () {
var type = Object.prototype.toString.call(this);
if (type === _bin.types.obj) {
if (toString.call(this) === types.obj) {
return Object.keys(this).length < 1;

@@ -222,25 +231,29 @@ }

func: function () {
return Object.prototype.toString.call(this) === _bin.types.func;
return toString.call(this) === types.func;
},
numeric: function () {
return Object.prototype.toString.call(this) === _bin.types.num && this === this;
return (toString.call(this) === types.num) &&
(this === this);
},
object: function () {
return Object.prototype.toString.call(this) === _bin.types.obj;
return toString.call(this) === types.obj;
},
size: function () {
var type = Object.prototype.toString.call(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;
var type = toString.call(this);
if (
type === types.arr ||
type === types.str ||
type === types.func
) return this.length;
else if (type === types.obj) return Object.keys(this).length;
else if (type === types.bool) return (this ? 1 : 0);
else if (type === types.num && this === this) return this;
},
string: function () {
return Object.prototype.toString.call(this) === _bin.types.str;
return toString.call(this) === types.str;
},
type: function () {
var t = Object.prototype.toString.call(this).match(/\w+(?=\])/)[0].toLowerCase();
return (t === 'number' && this !== this ? 'NaN' : t);
var type = toString.call(this).match(/\w+(?=\])/)[0].toLowerCase();
return (type === 'number' && this !== this ? 'NaN' : type);
}

@@ -254,35 +267,46 @@ }, function (key) {

capitalize: function () {
return this.substring(0, 1).toUpperCase() + this.substring(1);
return this.charAt(0).toUpperCase() + this.substring(1);
},
chars: function () {
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++;
var output = [],
length = this.length,
index = 0,
current;
for (; index < length; index++) {
current = this.substr(index, 2);
if (
regex.surrogatePair.test(current) ||
regex.symbolWithCombiningMarks.test(current)
) {
output.push(current);
index++;
} else {
r.push(this.substr(i, 1));
output.push(this.substr(index, 1));
}
}
return r;
return output;
},
chop: function () {
var c = this.substr(this.length - 2, 2);
var length = this.length,
end = this.substr(length - 2, 2);
if (_bin.regex.surrogatePair.test(c) ||
_bin.regex.symbolWithCombiningMarks.test(c)) {
return this.substr(0, this.length - 2);
if (
regex.surrogatePair.test(end) ||
regex.symbolWithCombiningMarks.test(end)
) {
return this.substr(0, length - 2);
} else {
return this.substring(0, this.length - 1);
return this.substring(0, length - 1);
}
},
chr: function () {
var c = this.substring(0, 2);
if (_bin.regex.surrogatePair.test(c) ||
_bin.regex.symbolWithCombiningMarks.test(c)) {
return c;
var front = this.substring(0, 2);
if (regex.surrogatePair.test(front) ||
regex.symbolWithCombiningMarks.test(front)) {
return front;
} else {
return c.substring(0, 1);
return this.charAt(0);
}

@@ -298,9 +322,17 @@ },

// Credit Mathias Bynens, github.com/mathiasbynens/esrever
function rev (s) {
s = s.replace(_bin.regex.symbolWithCombiningMarks, function($0, $1, $2) {
function rev (string) {
var output = '',
index;
string = string
.replace(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;
})
.replace(regex.surrogatePair, '$2$1');
index = string.length;
while (index--) output += string.charAt(index);
return output;
}

@@ -313,8 +345,13 @@

swapcase: function () {
var s = '', i, c;
for (i = 0; i < this.length; i++) {
c = this.charAt(i).toUpperCase();
s += (c === this.charAt(i) ? c.toLowerCase() : c);
var output = '',
length = this.length,
index = 0,
current, up;
for (; index < length; index++) {
current = this[index];
up = current.toUpperCase();
output += (current === up ? current.toLowerCase() : up);
}
return s;
return output;
},

@@ -328,27 +365,49 @@ upcase: function () {

assoc: function (key) {
for (var i = 0; i < this.length; i++) {
if (Object.prototype.toString.call(this[i]) !== _bin.types.obj) continue;
if (this[i].hasOwnProperty(key)) return this[i];
var length = this.length,
index = 0,
current;
for (; index < length; index++) {
current = this[index];
if (toString.call(current) !== types.obj) continue;
if (current.hasOwnProperty(key)) return current;
}
},
count: function (vfn, f) {
var args = Array.prototype.slice.call(arguments).length;
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');
for (i; i < this.length; i++) {
if ((t && vfn.call(this, this[i], i)) || (!t && this[i] === vfn)) c++;
count: function (obtest, usefunc) {
var argc = arguments.length,
count, index, length, element;
if (argc < 1) throw new Error('1 argument expected, got: ' + argc);
count = index = 0;
length = this.length;
usefunc = (typeof obtest === 'function') &&
(typeof usefunc === 'undefined' ? true : usefunc);
for (; index < length; index++) {
element = this[index];
if (
(usefunc && obtest.call(this, element, index)) ||
(!usefunc && element === obtest)
) count++;
}
return c;
return count;
},
cycle: function (n, fn, after) {
var args = Array.prototype.slice.call(arguments).length;
if (args < 2) {
throw new Error('cycle requires 2 arguments, ' + args + ' given');
}
if (typeof fn !== 'function') throw new TypeError('Expected function');
cycle: function (count, callback, after) {
var argc = arguments.length,
cycle, index, length;
for (var i = 1; i <= n; i++) {
for (var j = 0; j < this.length; j++) {
fn.call(this, this[j], j, i);
if (argc < 2)
throw new Error('cycle requires 2 arguments, ' + argc + ' given');
else if (typeof callback !== 'function')
throw new TypeError('Expected function');
length = this.length;
for (cycle = 1; cycle <= count; cycle++) {
for (index = 0; index < length; index++) {
callback.call(this, this[index], index, cycle);
}

@@ -359,14 +418,17 @@ }

},
delete: function (v) {
var args = Array.prototype.slice.call(arguments);
if (args.length < 1) throw new Error('No value given');
delete: function (value) {
var argc = arguments.length,
index, current, last;
var i = 0, last = null;
if (argc < 1) throw new Error('No value given');
while (i < this.length) {
if (this[i] === v) {
last = this[i];
this.splice(i, 1);
} else {
i++;
index = this.length;
last = null;
while (index--) {
current = this[index];
if (current === value) {
this.splice(index, 1);
last = current;
}

@@ -378,44 +440,79 @@ }

drop: Array.prototype.slice,
fetch: function(n, d) {
if (typeof n !== 'number' || !isFinite(n)) throw new TypeError('Expected number');
if (Math.abs(n) > this.length - 1 && typeof d === 'undefined') throw new RangeError('Index out of bounds');
if (n < 0) n = this.length + n;
return (typeof this[n] !== 'undefined' ? this[n] : d);
fetch: function (index, substitute) {
var length = this.length,
value;
if (typeof index !== 'number' || !isFinite(index))
throw new TypeError('Expected number');
else if (Math.abs(index) >= length && typeof substitute === 'undefined')
throw new RangeError('Index out of bounds');
if (index < 0)
index = length + index;
value = this[index];
return (typeof value !== 'undefined' ? value : substitute);
},
reject: function (f) {
var a = [], i;
for (i = 0; i < this.length; i++) {
if (!f.call(null, this[i], i)) a.push(this[i]);
reject: function (test) {
var output, length,
element, index;
if (typeof test !== 'function')
throw new TypeError('Excpected function');
output = [];
index = 0;
length = this.length;
for (; index < length; index++) {
element = this[index];
if (!test.call(this, element, index)) output.push(this[index]);
}
return a;
return output;
},
select: Array.prototype.filter,
take: function (n) {
return this.slice().splice(0, n);
take: function (length) {
return this.slice().splice(0, length);
},
valuesAt: function () {
var a = Array.prototype.slice.call(arguments),
o = [], i;
var argc = arguments.length,
length = this.length,
output = [],
index = 0,
position;
for (i = 0; i < a.length; i++) {
if (typeof a[i] !== 'number' || !isFinite(a[i])) throw new TypeError('Expected index');
if (a[i] < 0) a[i] = this.length + a[i];
o.push(this[a[i]]);
if (length) {
for (; index < argc; index++) {
position = arguments[index];
if (position < 0) {
position = length + position;
}
output.push(this[position]);
}
}
return o;
return output;
},
zip: function() {
var a = Array.prototype.slice.call(arguments),
o = [], i, j, q;
zip: function () {
var argc = arguments.length,
length = this.length,
output = [],
index = 0,
tempArray, argIndex;
for (i = 0; i < this.length; i++) {
q = [this[i]];
for(j = 0; j < a.length; j++) {
q.push(a[j][i]);
for (; index < length; index++) {
tempArray = [this[index]];
for (argIndex = 0; argIndex < argc; argIndex++) {
tempArray.push(arguments[argIndex][index]);
}
o.push(q);
output.push(tempArray);
}
return o;
return output;
}

@@ -429,35 +526,39 @@ });

major(Number.prototype, {
downto: function (n, fn, after) {
var type = Object.prototype.toString.call(n);
if (type !== _bin.types.num || n.toFixed() != n) {
downto: function (end, callback, after) {
var start = this;
if (toString.call(end) !== types.num || end.toFixed() != end)
throw new TypeError('Integer value required');
}
if (this.toFixed() != this || this === Infinity) {
else if (start != start.toFixed() || start === Infinity)
throw new TypeError('Self is not an integer');
}
if (typeof fn !== 'function') throw new TypeError('Expected function');
for (var i = this; i >= n; i--) {
fn.call(this, i);
}
else if (typeof callback !== 'function')
throw new TypeError('Expected function');
for (; start >= end; start--) callback.call(this, start);
return (typeof after === 'function'? after.call(this) : after);
},
times: function (fn, after) {
if (typeof fn !== 'function') throw new TypeError('Expected function');
for (var i = 1; i <= this; i++) {
fn.call(this, i);
}
times: function (callback, after) {
var start = 1,
end = this;
if (typeof callback !== 'function')
throw new TypeError('Expected function');
for (; start <= end; start++) callback.call(this, start);
return (typeof after === 'function'? after.call(this) : after);
},
upto: function (n, fn, after) {
var type = Object.prototype.toString.call(n);
if (type !== _bin.types.num || n.toFixed() != n) {
upto: function (end, callback, after) {
var start = this;
if (toString.call(end) !== types.num || end.toFixed() != end)
throw new TypeError('Integer value required');
}
if (this.toFixed() != this || this === Infinity) {
else if (start != start.toFixed() || start === Infinity)
throw new TypeError('Self is not an integer');
}
if (typeof fn !== 'function') throw new TypeError('Expected function');
for (var i = this; i <= n; i++) {
fn.call(this, i);
}
else if (typeof callback !== 'function')
throw new TypeError('Expected function');
for (; start <= end; start++) callback.call(this, start);
return (typeof after === 'function'? after.call(this) : after);

@@ -468,11 +569,19 @@ }

major(Object.prototype, {
each: function(fn, after) {
var type = Object.prototype.toString.call(this);
if (type !== _bin.types.obj) return;
if (typeof fn !== 'function') throw new TypeError('Expected function');
for (var k in this) {
if (this.hasOwnProperty(k)) {
fn.call(this, k, this[k]);
}
each: function (callback, after) {
var keys, length, index, property;
if (toString.call(this) !== types.obj)
return;
else if (typeof callback !== 'function')
throw new TypeError('Expected function.');
keys = Object.keys(this);
length = keys.length;
index = 0;
for (; index < length; index++) {
property = keys[index];
callback.call(this, property, this[property]);
}
return (typeof after === 'function'? after.call(this) : after);

@@ -483,11 +592,12 @@ },

},
fetch: function (v, r) {
var type = Object.prototype.toString.call(this),
args = Array.prototype.slice.call(arguments);
if (type !== _bin.types.obj) return;
if (args.length < 1) throw new Error('No value given');
fetch: function (key, substitute) {
if (toString.call(this) !== types.obj)
return;
else if (arguments.length < 1)
throw new Error('No value given');
if (this.hasOwnProperty(v)) return this[v];
if (this.hasOwnProperty(key)) return this[key];
return (typeof r === 'function' ? r.call(this, v) : r);
return (typeof substitute === 'function' ?
substitute.call(this, key) : substitute);
}

@@ -497,7 +607,16 @@ });

major(String.prototype, {
each: function (fn, after) {
if (typeof fn !== 'function') throw new TypeError('Expected function');
for (var i = 0; i < this.length; i++) {
fn.call(this, this[i], i);
each: function (callback, after) {
var length,
index;
if (typeof callback !== 'function')
throw new TypeError('Expected function');
length = this.length;
index = 0;
for (; index < length; index++) {
callback.call(this, this.charAt(index), index);
}
return (typeof after === 'function'? after.call(this) : after);

@@ -510,2 +629,2 @@ },

}((typeof window !== 'undefined' ? window : global)));
}(this));

@@ -87,3 +87,3 @@ var chai = require('chai'),

expect([true, true, false, false].uniq).to.deep.equal([true, false]);
})
});

@@ -141,3 +141,3 @@ it('should work on string values', function () {

it('should not use it as a filter if the section parameter is false', function () {
it('should not use it as a filter if the second parameter is false', function () {
expect(a.count(test, false)).to.equal(1);

@@ -171,3 +171,3 @@ });

a.should.deep.equal([1, 2]);
})
});
});

@@ -203,3 +203,3 @@

bar.should.not.equal(foo);
})
});
});

@@ -222,4 +222,4 @@

it('should throw RangeError on out of bounds index, with no substitute', function () {
expect(function () {foo.fetch(10)}).to.throw(RangeError);
expect(function () {foo.fetch(-10)}).to.throw(RangeError);
expect(function () {foo.fetch(10);}).to.throw(RangeError);
expect(function () {foo.fetch(-10);}).to.throw(RangeError);
});

@@ -245,2 +245,11 @@

});
it('should throw a type error when passed a non-function', function () {
expect(function () {foo.reject(42);}).to.throw(TypeError);
expect(function () {foo.reject('');}).to.throw(TypeError);
expect(function () {foo.reject(true);}).to.throw(TypeError);
expect(function () {foo.reject(null);}).to.throw(TypeError);
expect(function () {foo.reject({});}).to.throw(TypeError);
expect(function () {foo.reject();}).to.throw(TypeError);
});
});

@@ -281,2 +290,8 @@

});
it('should work for negative values', function () {
var qar = foo.valuesAt(0, -1, -2);
assert.deepEqual(qar, [1, 3, 'baz']);
});
});

@@ -297,2 +312,2 @@

});
});
});

@@ -0,0 +0,0 @@ var chai = require('chai'),

@@ -0,0 +0,0 @@ var chai = require('chai'),

@@ -129,3 +129,3 @@ var chai = require('chai'),

});
});
});

@@ -283,2 +283,2 @@ describe('#polar', function () {

});
});
});

@@ -0,0 +0,0 @@ var chai = require('chai'),

@@ -0,0 +0,0 @@ var chai = require('chai'),

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc