Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "rubyisms", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Prototype extensions.", | ||
@@ -11,4 +11,6 @@ "main": "rubyisms.js", | ||
"scripts": { | ||
"test": "npm run test-arrays && npm run test-numbers && npm run test-objects && npm run test-strings", | ||
"test": "npm run test-arrays && npm run test-booleans && npm run test-functions && npm run test-numbers && npm run test-objects && npm run test-strings", | ||
"test-arrays": "mocha test/arrays.js", | ||
"test-booleans": "mocha test/booleans.js", | ||
"test-functions": "mocha test/functions.js", | ||
"test-numbers": "mocha test/numbers.js", | ||
@@ -15,0 +17,0 @@ "test-objects": "mocha test/objects.js", |
@@ -1,3 +0,19 @@ | ||
# Rubyisms | ||
<p align="center"> | ||
<img src="http://i.imgur.com/Gx7OFGO.png" /> | ||
</p> | ||
Prototype extensions. | ||
# rubyisms | ||
**Ruby style ES5 prototype extensions** | ||
## info | ||
A highly experimental set of prototype extensions for types in ES5 JavaScript. | ||
## install | ||
Using [npm](npm). | ||
npm install rubyisms | ||
[npm]: https://www.npmjs.com/ |
157
rubyisms.js
@@ -9,34 +9,13 @@ // Colin 'Oka' Hall-Coates | ||
var def = Object.defineProperties, | ||
AP = Array.prototype, | ||
BP = Boolean.prototype, | ||
FP = Function.prototype, | ||
NP = Number.prototype, | ||
OP = Object.prototype, | ||
AP = Array.prototype, | ||
SP = String.prototype, | ||
NP = Number.prototype; | ||
SP = String.prototype; | ||
function $(g, s) {return {get: g, set: s};} | ||
def(SP, { | ||
capitalize: $(function () { | ||
return this.substring(0, 1).toUpperCase() + this.substring(1); | ||
}), | ||
chars: $(function () { | ||
return this.split(''); | ||
}), | ||
chr: $(function () { | ||
return this.substring(0, 1); | ||
}), | ||
downcase: $(function () { | ||
return this.toLowerCase(); | ||
}), | ||
empty: $(function () { | ||
return this.length < 1; | ||
}), | ||
reverse: $(function () { | ||
var r = '', i = this.length - 1, e; | ||
for (e = 0; i >= e; i--) r += this[i]; | ||
return r; | ||
}), | ||
upcase: $(function () { | ||
return this.toUpperCase(); | ||
}) | ||
}); | ||
// Minor methods | ||
@@ -60,17 +39,48 @@ def(AP, { | ||
def(BP, { | ||
not: $(function () { | ||
return !this; | ||
}) | ||
}); | ||
def(FP, {}); | ||
def(NP, { | ||
abs: $(function () { | ||
return Math.abs(this); | ||
}), | ||
finite: $(function () { | ||
return isFinite(this); | ||
}), | ||
floor: $(function () { | ||
return Math.floor(this); | ||
}), | ||
integer: $(function () { | ||
return this.toFixed() == this && this !== Infinity; | ||
}), | ||
polar: $(function () { | ||
return (isFinite(this) && | ||
[Math.abs(this), (this < 0 ? Math.PI : 0)]) || | ||
undefined; | ||
}), | ||
round: $(function () { | ||
return Math.round(this); | ||
}) | ||
}); | ||
def(OP, { | ||
array: $(function () { | ||
return Array.isArray(this); | ||
return OP.toString.call(this) === '[object Array]'; | ||
}), | ||
bool: $(function () { | ||
return typeof this === 'boolean'; | ||
return OP.toString.call(this) === '[object Boolean]'; | ||
}), | ||
func: $(function () { | ||
return typeof this === 'function'; | ||
return OP.toString.call(this) === '[object Function]'; | ||
}), | ||
numeric: $(function () { | ||
return typeof this === 'number' && this === this; | ||
return OP.toString.call(this) === '[object Number]' && this === this; | ||
}), | ||
object: $(function () { | ||
return !Array.isArray(this) && typeof this === 'object'; | ||
return OP.toString.call(this) === '[object Object]'; | ||
}), | ||
@@ -85,35 +95,51 @@ size: $(function () { | ||
string: $(function () { | ||
return typeof this === 'string' || this instanceof String; | ||
return OP.toString.call(this) === '[object String]'; | ||
}), | ||
type: $(function () { | ||
var t = OP.toString.call(this) | ||
switch(t) { | ||
case '[object Array]': | ||
return 'array'; | ||
case '[object Boolean]': | ||
return 'boolean'; | ||
case '[object Function]': | ||
return 'function'; | ||
case '[object Number]': | ||
if (this === this) return 'number'; | ||
else return 'NaN'; | ||
case '[object Object]': | ||
return 'object'; | ||
case '[object String]': | ||
return 'string'; | ||
} | ||
}) | ||
}); | ||
def(NP, { | ||
abs: $(function () { | ||
return Math.abs(this); | ||
def(SP, { | ||
capitalize: $(function () { | ||
return this.substring(0, 1).toUpperCase() + this.substring(1); | ||
}), | ||
finite: $(function () { | ||
return isFinite(this); | ||
chars: $(function () { | ||
return this.split(''); | ||
}), | ||
floor: $(function () { | ||
return Math.floor(this); | ||
chr: $(function () { | ||
return this.substring(0, 1); | ||
}), | ||
integer: $(function () { | ||
return this.toFixed() == this && this !== Infinity; | ||
downcase: $(function () { | ||
return this.toLowerCase(); | ||
}), | ||
polar: $(function () { | ||
return (isFinite(this) && | ||
[Math.abs(this), (this < 0 ? Math.PI : 0)]) || | ||
undefined; | ||
empty: $(function () { | ||
return this.length < 1; | ||
}), | ||
round: $(function () { | ||
return Math.round(this); | ||
reverse: $(function () { | ||
var r = '', i = this.length - 1, e; | ||
for (e = 0; i >= e; i--) r += this[i]; | ||
return r; | ||
}), | ||
upcase: $(function () { | ||
return this.toUpperCase(); | ||
}) | ||
}); | ||
var _String = { | ||
_p: SP, | ||
prepend: function (o) { | ||
return o + this; | ||
} | ||
}; | ||
// Major methods | ||
@@ -164,2 +190,14 @@ var _Array = { | ||
var _Boolean = { | ||
_p: BP | ||
}; | ||
var _Function = { | ||
_p: FP | ||
}; | ||
var _Number = { | ||
_p: NP | ||
}; | ||
var _Object = { | ||
@@ -172,4 +210,7 @@ _p: OP, | ||
var _Number = { | ||
_p: NP | ||
var _String = { | ||
_p: SP, | ||
prepend: function (o) { | ||
return o + this; | ||
} | ||
}; | ||
@@ -184,4 +225,4 @@ | ||
[_String, _Array, _Object, _Number].forEach(buildPrototypes); | ||
[_Array, _Boolean, _Function, _Number, _Object, _String].forEach(buildPrototypes); | ||
}()); |
@@ -108,2 +108,15 @@ var chai = require('chai'), | ||
describe('#type', function () { | ||
it('should return a type string', function () { | ||
expect(a.type).to.equal('array'); | ||
expect(b.type).to.equal('boolean'); | ||
expect(f.type).to.equal('function'); | ||
expect(n.type).to.equal('number'); | ||
expect(o.type).to.equal('object'); | ||
expect(s.type).to.equal('string'); | ||
expect(NaN.type).to.equal('NaN'); | ||
expect(Infinity.type).to.equal('number'); | ||
}); | ||
}); | ||
// Major methods | ||
@@ -110,0 +123,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21164
11
600
19
0