Rearmed-JS
This library is a collection of helpful methods and monkey patches for Arrays, Objects, Numbers, and Strings in Javascript. Start writing your Javascript like you write your Ruby code!
Works in the Browser and in NodeJS.
5.1kb minified and gzipped.
Install
Yarn, NPM, Bower
yarn add rearmed-js
npm install rearmed-js
bower install rearmed-js
Rails / Bundler
# Gemfile source 'https://rails-assets.org' do end
Usage
Note: Only import/require the patches once at the beginning of your page or app start otherwise you will get warnings about redefining methods.
Plain HTML
<!-- OR only the patches you want --> <!-- All patches for a certain object type only --> <!-- OR only the methods you want -->
ES7+
; // import everything // OR // import patches for certain object types only;;;;; // OR // import only the methods you want;;
ES6
// require everything // OR // require patches for certain object types only;;;;; // OR // require only the methods you want;;
Rails
/* *= * * OR for certain object types only *= *= *= *= *= * * OR only the methods you want *= *= */
Methods Implemented
Generic
These methods are available on all types that inherit from Object which is almost everything.
var { };var str = "";var array = ; str; // returns bool str; // returns bool// collections are blank if length == 0 str; // return bool// collections are present if length > 0 array; // returns self or false// same thing as: array.isPresent() ? array : false; cb; // return str// possible return values are 'Object','Array','String','Boolean','Number','Function','Other' // return value or falsestr; // => 0array; // => []str; // => false
Array
var array = ; var { }; array // returns bool arrayallcb=null // returns bool arraycompactbadValues=null undefined '' // returns array, accepts array or splat arguments array // returns value, accepts splat arguments or array array array // return bool array // returns bool array // returns bool array // returns value, undefined if not found array // returns integer, undefined if not found array // returns value array // returns array array // returns bool array // returns nested array array // returns nested array arraylast // returns value array // returns value array // returns value array // returns value array // returns value array // reutrns array array // returns array array; // returns array// this is an each method that returns the original array after its done array // returns bool// smart meaning that it uses `equals` method to compare if item is Array or Object array // returns bool// smart meaning that it uses `equals` method to compare if item is Array or Object array // returns number array // returns array /* CLASS METHODS - Not available in Typescript */ Array // returns array
Object (Hash)
var obj = {}; // Monkey-patching Object is very dangerous, so we only patch it with one methodobj = obj; var { }; objallcb=null // returns bool obj // returns bool objcompactbadValues=null undefined '' // returns object, accepts array or splat arguments obj // returns object, accepts splat arguments or array obj; obj // returns bool obj // returns bool obj // returns object, accepts keys as splat arguments or an array obj // returns bool obj // returns bool obj // returns string obj // returns array obj // returns object obj // returns object, accepts keys as splat arguments or an array obj // returns object obj // returns object, known to cause error with React select elements obj // returns array
When requiring Object patches individually you must first require object/rearmed
. Example:
;;;
The following methods are used under the hood to provide patches to Object. I have exposed these methods publicly so that you can add your own custom methods to rearmed()
objects. These methods are available on Object after requiring either of these: rearmed-js/object/rearmed
or rearmed-js
Objectrearmed
Number
var num = 85; num // returns number num // returns number num // returns bool num // returns bool num // returns bool num // returns bool num // returns number
String
var str = 'Hello World': str // returns string str // returns bool str // returns array str // returns string str // returns bool str // returns bool str // returns bool str // returns string str // returns bool str // returns string str // returns string str // returns string str // returns bool str // returns string str // returns string// Warning: doesn't warn when it overwrites the original sub method as it has been removed from the JS standard. str // returns string str // returns bool str // returns string
Browser / NodeJS / Typescript Support
- Browser support is IE 9+ and everything else. Use the files in the
dist
folder if you need pre-minified files. - Array
find
doesnt work properly in old NodeJS 0.x and iojs. - String
empty
doesnt work properly in old NodeJS 0.x. - Typescript is supported however it does not support Class methods because its not possible to define static methods on existing interfaces such as Array. At this time the only effected method is
Array.range
Contributing
- I recommend discussing your intentions via an issue before making a PR as they are very concious design choices that must go into this library.
- Only edit js files from
src/
andtest/
folders. - Use the gulp task:
gulp
to run the build after making your changes. - Testing these methods are simple & easy. PR's should include tests.
Credits
Created by Weston Ganger - @westonganger
For any consulting or contract work please contact me via my company website: Solid Foundation Web Development