string-etc
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "string-etc", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/dicksont/string-etc", | ||
@@ -5,0 +5,0 @@ "authors": [ |
{ | ||
"name": "string-etc", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Collection of supplementary JavaScript String methods that works across module packaging systems and that can be included piece-wise ", | ||
"main": "node/loader.js", | ||
"main": "node/etc.js", | ||
"directories": { | ||
@@ -13,3 +13,3 @@ "test": "test" | ||
"scripts": { | ||
"test": "mocha test/test_wrapper.js test/test_cram.js" | ||
"test": "mocha test/test_wrapper.js test/test_loader.js test/test_cram.js test/test_pad.js" | ||
}, | ||
@@ -16,0 +16,0 @@ "repository": { |
@@ -9,2 +9,3 @@ [![Build Status](https://travis-ci.org/dicksont/string-etc.svg?branch=master)](https://travis-ci.org/dicksont/string-etc) | ||
## Usage | ||
### Web page | ||
Accessing these extensions will differ depending on our module system. If we are on *Browser* or *AMD*, we can access these methods directly from an *String* object. For example, we can do: | ||
@@ -16,4 +17,6 @@ | ||
However, if we are on *CommonJS/Node*, we have to access these methods through a wrapper. So instead of the call above, we would have: | ||
### Node wrapper | ||
However, if we are on *CommonJS/Node*, we can access these methods through a wrapper. So instead of the call above, we would have: | ||
```javascript | ||
@@ -26,10 +29,21 @@ string('California').cram(8) // returns Califor… | ||
```javascript | ||
var string = require('array-etc')(['cram']); | ||
var string = require('array-etc').wrap(['cram']); | ||
``` | ||
This extra wrapper is a special arrangement we added on Node, in order to avoid global conflicts with the *String.prototype* object. We may have other libraries or other versions of this library in our dependency tree. Unbeknownst to us, these libraries may add methods of similar names to the *String.prototype* object. We must attach the methods locally to a wrapping function to avoid these potential collisions. | ||
This extra wrapper is a special arrangement we added on Node, in order to avoid global conflicts with the *String.prototype* object. We may have other libraries or other versions of this library in our dependency tree. Unbeknownst to us, these libraries may add methods of similar names to the *String.prototype* object. We can attach the methods locally to a wrapping function to avoid these potential collisions. | ||
Since unintentional collisions are a lot harder on Browser or with AMD, where the end developer actively controls the loading of the modules, we have not seen a case for extending the wrapping function to these systems. | ||
Since unintentional 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 seen a case for extending the wrapping function to these systems. | ||
### Node loader | ||
Using a wrapper on Node ensures safety. However, it does introduce a speed bump in that a extra function call must be required before the string can be operated. Calls to library methods with a single source string would require nesting. | ||
If safety is not an issue, you can use the direct syntax in Node as well with a loader call. For example: | ||
```javascript | ||
require('array-etc').load(['cram']); | ||
``` | ||
This will load **cram** into **String.prototype** | ||
## Installation | ||
@@ -59,8 +73,14 @@ ### Web page | ||
In your file, write | ||
In your file, require the library, and call wrap with the methods you want your wrapper have. For example: | ||
```javascript | ||
var strload = require('string-etc'); | ||
var string = strload(['cram']); | ||
var string = require('string-etc').wrap(['cram']); | ||
``` | ||
This creates a custom wrapper function, which you can use to access the individual methods. | ||
Or if you prefer the direct syntax without wrapping: | ||
```javascript | ||
require('string-etc').load(['cram']); | ||
``` | ||
## Libraries | ||
### lib/cram.js | ||
Cram takes a string and shortens it, by replacing |
@@ -29,18 +29,10 @@ /* | ||
var arrayFactory, assert; | ||
if (typeof module !== 'undefined' && module && module.exports) { // Node.js & CommonJS | ||
createWrapper = function() { return require('../node/wrapper.js')('cram'); } | ||
assert = require('assert'); | ||
factory(require('assert'), function() { return require('../node/etc.js').wrap('cram'); }); | ||
} else { | ||
assert = window.assert; | ||
createWrapper = function() { | ||
return function(obj) { | ||
return obj; | ||
} | ||
} | ||
factory(window.assert, function() { return function(obj) { return obj; } }); | ||
mocha.checkLeaks(); | ||
mocha.run(); | ||
} | ||
factory(assert, createWrapper); | ||
})(function(assert, createWrapper) { | ||
@@ -47,0 +39,0 @@ describe('String.prototype.cram', function() { |
@@ -28,7 +28,7 @@ /* | ||
var assert = require('assert'); | ||
var fawrap = require('../node/wrapper.js'); | ||
var fawrap = require('../node/etc.js').wrap; | ||
describe('Wrapper', function() { | ||
it('can load library via string', function() { | ||
it('can import library via string', function() { | ||
var wrapper = fawrap('cram'); | ||
@@ -40,3 +40,3 @@ assert.ok(wrapper.hasOwnProperty('cram')); | ||
it('can load libraries via Array', function() { | ||
it('can import libraries via Array', function() { | ||
var wrapper = fawrap(['cram']); | ||
@@ -43,0 +43,0 @@ assert.ok(wrapper.hasOwnProperty('cram')); |
Sorry, the diff of this file is not supported yet
25016
13
441
83