npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ruby-nice

v0.3.2

Published

The nice javascript library to rubynize your javascript to be a happy programmer again.

Downloads

1,416

Readme

ruby-nice

npm package GitHub release (latest by date) downloads License: MIT

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_objectMyClass.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.