stringish

Helper for creating string backed objects

Usage no npm install needed!

<script type="module">
  import stringish from 'https://cdn.skypack.dev/stringish';
</script>

README

stringish

Helper for creating string backed objects

Build Status Coverage Status Code Climate Dependency Status devDependency Status

NPM

Wrap strings and provide additional information. This is especially useful for parser tokens, or anytime the backing data is just a string, but you want to add sugar methods that provide additional functionality.

Usage

var stringish = require('stringish');
var assert = require('assert');

function MyParserToken(backingString) {
  this._stringValue = backingString;
}

stringish(MyParserToken.prototype); // adds `toString`, `valueOf`, and some delegate methods.

var token = new MyParserToken('Token Value');

// this is the useful bit
assert(token instanceof MyParserToken);

// toString just returns what you put in.
assert.equal(token.toString(), 'Token Value');

// toValue is overridden, so you can do non-strict comparison
assert.equal(token, 'Token Value');
  
// can be used with regularExpressions
assert(/Value$/.test(token));
assert.equal(/^\w+/.exec(token)[0], 'Token');

Each instance has the following delegate methods that will be called on the underlying String.

  • charAt
  • charCodeAt
  • concat
  • indexOf
  • lastIndexOf
  • match
  • replace
  • search
  • slice
  • split
  • substr
  • substring
  • toLowerCase
  • toUpperCase
  • trim

API

stringish(proto [, propertyName])

Adds a number of functions to ConstructorFn.prototype, including toString, valueOf, and a host of methods that delegate directly to the string.

ConstructorFn

Required
type: function

An object that you want to add the delegate functions to, usually a prototype value.

propertyName

Optional type: string

The property name of the string you want to delegate to. Defaults to _stringValue.

License

MIT © James Talmage