Collection of supplementary JavaScript String methods that works across module packaging systems and that can be included piece-wise
npm install string-etc


A collection of handy String methods that works across various JavaScript module systems, including:
- Browser script tag
- CommonJS / Node.js
- AMD / Require.js.
Methods can be included piecewise.
``javascript`
'California'.cram(8) // returns Califor…
However, this type of direct access may lead to conflicts on CommonJS systems like Node. Unlike AMD or web pages, libraries are automatically loaded in Node. We may have other libraries in our dependency tree that add similarly named methods to the same String.prototype object. When we call these methods, we may be in fact be calling the function that overwrote ours.
We added a special arrangement on Node to prevent this problem. We create a local wrapper function. Instead of attaching the methods to the global String.prototype object, we attach them to this function. We would have:
`javascript`
string('California').cram(8) // returns Califor…
where string is a constructed function that wraps around our String.
`javascript`
var string = require('array-etc').wrap(['cram']);
Since these kind of unintended collisions are a lot harder with script tag loads or AMD, where the end developer actively controls the loading of the modules, we have not extended the wrapper function to these systems.
The wrapper function on Node guarantees safety. However, it may be harder to manipulate. We need an extra function call for every string operation, and string operations cannot be chained as elegantly.
If safety is not an issue, we can use the direct syntax in Node as well. We just need to call load instead of wrap in our setup.
For example:
`javascript`
require('array-etc').load(['cram']);
will load cram into String.prototype
bower install string-etc
`If we use npm, we would run:
`
npm install string-etc
`Then, we add the corresponding script tag to our page. e.g.
`html
`$3
In our shell, run:
`shell
npm install string-etc
`In our file, require string-etc.
Call wrap with the methods we desire. For example:
`javascript
var string = require('string-etc').wrap(['cram']);
`Or call load if we prefer the direct syntax without wrapping:
`javascript
require('string-etc').load(['cram']);
`Libraries
$3
String.prototype.cram(space, opts)
Cram tries to fit a string within a given width, by replacing excess characters with an ellipsis:For example, suppose we are building a web page that indexes different Objective-C methods. Objective-C has some pretty long function names that might mess up our layout. Using cram, we can shorten these. Let's try a pretty bad case:
`javascript
"splitViewController:willHideViewController:withBarButtonItem:forPopoverController:".cram(); //"splitViewContro…"
`By default, cram returns a string with max length 16. However, we can adjust this by passing in a number:
`javascript
"splitViewController:willHideViewController:withBarButtonItem:forPopoverController:".cram(8); //"splitVi…"
``E-mail me:
- if you have problems or questions.
- if you find a String method that you think should be included, because it would be useful for others as well.
- if you have an interesting use case that I should consider