ruby-nice
v0.3.2
Published
The nice javascript library to rubynize your javascript to be a happy programmer again.
Downloads
1,332
Maintainers
Readme
ruby-nice
The nice javascript library to rubynize your javascript to be a happy programmer again.
This library is mainly intended for those who are familiar with programming with Ruby and miss some convenience features in Javascript. It's time to be happy again!
On the one hand it contains extensions and patches for core classes (e.g. String),
on the other hand it provides (extended) wrapper classes in Ruby style,
e.g. File
with File.write()
.
It is available for the browser as well as for NodeJS.
Browser version might be behind npm version, if there were only node related updates, e.g. file system access.
Search terms:
ruby for javascript, ruby methods for javascript, ruby functions for javascript
Table of contents
Usage
JavaScript method naming of the Ruby ports
The javascript method names are ported to a javascript equivalent by the following rules and always written
in camelCase
:
| Description | Ruby code | JavaScript code |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|-------------------------------------------------|
| Question mark methods are ported to isMethod
| File.exist?
File.directory?
| File.isExisting()
File.isDirectory()
|
| Getters are ported to getMethod getMethod
| File.basename
| File.getBaseName()
|
| Verbs and transformation methods starting with 'to' are only ported to camelCase
| MyClass.destroy_object
MyClass.to_hash
| MyClass.destroyObject()
MyClass.toHash()
|
| Loops should start with for
, but they would collide with java script methods, e.g. forEach()In cases of collissions, the orignal ruby name remains if possible. Some other cases have new names. | [].each
/ [].each_with_index
| [1,2,3].eachWithIndex()
|
Usage example
// -- node js CommonJS --
require('ruby-nice/array'); // only monkey patch arrays
require('ruby-nice/string'); // only monkey patch strings
require('ruby-nice'); // requiring from 'ruby-nice' will implicitely load all monkey patches at once
const File = require('ruby-nice/file'); // load this ported ruby class only
// or named import (will also implicitely load all monkey patches)
const { File } = require('ruby-nice'); // load ported ruby class
// -- node js ESM modules --
import { RubyNice } from 'ruby-nice'; // requiring from 'ruby-nice' will implicitely load all monkey patches at once
import { File } from 'ruby-nice'; // load ported ruby class (will also implicitely load all monkey patches)
import File from 'ruby-nice/file'; // load this ported ruby class only
// -- browser --
<script type="text/javascript" src="js/lib/ruby-nice.bundle.js"></script>
// -- code samples --
"sample".capitalize() // capitalize a string
// => "Sample"
[1,2,3].getSample() // get random element of an Array
// => 3
// iterate array
['dog','house','mouse'].eachWithIndex((val, i) => {
console.log(i + ':' + val);
});
// => 0:dog
// => 1:house
// => 2:mouse
// iterate object
{ peter: { role: "admin" }, sam: { role: "dev" } }.eachWithIndex((key, val, i) => {
console.log(key + " has the role: " + val.role);
});
// => peter has the role admin
// => sam has the role dev
// write text file
File.write("/home/user/document.txt", "some content");
// use map() on object
{ a: 1, b: 2}.mapObject((key, value, index) => {
return value;
})
// => [1,2]
Installation
NodeJS
You can either use npm or yarn to install ruby-nice.
yarn
In your project root directory execute the following command:
yarn add ruby-nice
npm
In your project root directory execute the following command:
npm install ruby-nice
Browser
Download the latest release on Github or the from the folder dist
and put it in an appropriate folder of your project, e.g. js/lib
and reference it by a script tag in your project:
<script type="text/javascript" src="js/lib/ruby-nice.bundle.js"></script>
Optionally you may add the source file to your build pipeline, if you are using webpack, brunch or any other packager.
Bundle releases
As ruby-nice
depends on Typifier, there is also a bundle release called ruby-nice.bundle.js
where the latter is included. If you already use Typifier separately, use the default version ruby-nice.js
without included dependencies. If you don't know what you should use, use the bundled release!
Minified releases
If you prefer minified builds, use the *.min.js
version. Be aware that they do not contain any javascript documentation that may be very useful when working with a powerful IDE.
Documentation
Feature set
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/magynhard/ruby-nice. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.