export default class PercentStrings {
constructor () {
this.w = w;
this.percent_w = percent_w;
}
}
Inspired by Ruby’s %w
.
Takes in a string as an argument and converts it to an array, deliminated by spaces.
import {w} from 'ruby';
let words = w('Hercules Mulligan!');
words; // ['Hercules', 'Mulligan!'];
Takes an options hash where you can specify a specific delimeter. Use this if you need an element to contain whitespaces, as escaping the white space character will not work.
let my_shot = 'I am not throwing away my shot.';
let words = w(my_shot, { delimiter: 'not' });
words; // ['I am ', ' throwing away my shot.'];
function w (words, options={}) {
let delimiter = options.delimiter || ' ';
let wordsArray = words.split(delimiter);
return wordsArray;
}
An alias for w. Javascript doesn’t allow methods to start with a %
sign 😢
import {percent_w} from 'ruby';
let you_say = percent_w('You say the price of my love\'s not a price that you\'re willing to pay.');
you_say; // ['You', 'say', 'the', 'price', 'of', 'my', 'love\'s', 'not', 'a', 'price', 'that', 'you\'re', 'willing', 'to', 'pay.']
let percent_w = w;