String
All strings attached!

Methods

These can be called through the str object on ruby:

ruby.str.downcase('hEllO'); // hello

Or, if you call the add_methods_to_string_prototype method, you can call each applicable method as a property on any string.

ruby = require('ruby');
ruby.add_methods_to_string_prototype();

'hEllO'.downcase; // hello

The examples below will always use the prototype version, unless otherwise specified.

export default class _String {
  constructor () {
    this.chars = chars;
    this.downcase = downcase;
    this.empty = empty;
    this.empty_questionmark = this.empty;
    this.reverse = reverse;
    this.upcase = upcase;
  }

new

Creates a new string.

Only available on ruby.str, not as a method on an instance of String.

let new_string = ruby.str.new('abc');
new_string; // 'abc'
  new (str) {
    return `${str}`;
  }

chars

Returns the characters of the string in an array.

let chars = 'abc'.chars;
chars; // ['a', 'b', 'c']
function chars(str) {
  str = str || this;

  return str.split('');
}

downcase

Returns the string with all lowercase letters.

let str = 'hEllO'.downcase;
str; // 'hello'
function downcase(str) {
  str = str || this;

  return str.toLowerCase();
}

empty

Returns true if the string is empty and false if it is not.

'hello'.empty; // false
' '.empty; // false
''.empty; // true

empty_questionmark is an alias for empty.

function empty(str) {
  if (str === undefined) { // eslint-disable-line no-undefined
    str = this;
  }

  return str.length === 0;
}

reverse

Returns the string in reverse order.

let str = 'stressed'.reverse;
str; // 'desserts'
function reverse(str) {
  str = str || this;

  let parts = str.split('');
  let reversedParts = parts.reverse();
  let reversed = reversedParts.join('');

  return reversed;
}

upcase

Returns the string with all uppercase letters.

let str = 'hEllO'.upcase;
str; // 'HELLO'
function upcase(str) {
  str = str || this;

  return str.toUpperCase();
}