Skip to content

Commit

Permalink
[Breaking] conform to the es-shim API.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 7, 2016
1 parent ee96020 commit 0727d38
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ String.prototype.trimLeft <sup>[![Version Badge][npm-version-svg]][package-url]<

[![browser support][testling-svg]][testling-url]

An ES7 spec-compliant `String.prototype.trimLeft` shim. Invoke its "shim" method to shim `String.prototype.trimLeft` if it is unavailable.
A spec-proposal-compliant `String.prototype.trimLeft` shim. Invoke its "shim" method to shim `String.prototype.trimLeft` if it is unavailable.

This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec](http://www.ecma-international.org/ecma-262/6.0/#sec-object.assign). In an ES6 environment, it will also work properly with `Symbol`s.

Most common usage:
```js
Expand Down
10 changes: 10 additions & 0 deletions implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var bind = require('function-bind');
var replace = bind.call(Function.call, String.prototype.replace);

var leftWhitespace = /^[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*/;

module.exports = function trimLeft() {
return replace(this, leftWhitespace, '');
};
26 changes: 9 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

var bind = require('function-bind');
var define = require('define-properties');
var replace = bind.call(Function.call, String.prototype.replace);

var leftWhitespace = /^[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*/;
var implementation = require('./implementation');
var getPolyfill = require('./polyfill');
var shim = require('./shim');

var trimLeft = function trimLeft() {
return replace(this, leftWhitespace, '');
};
var bound = bind.call(Function.call, getPolyfill());

var boundTrimLeft = bind.call(Function.call, trimLeft);
define(boundTrimLeft, {
shim: function shimTrimLeft() {
var zeroWidthSpace = '\u200b';
define(String.prototype, { trimLeft: trimLeft }, {
trimLeft: function () {
return zeroWidthSpace.trimLeft() !== zeroWidthSpace;
}
});
return String.prototype.trimLeft;
}
define(bound, {
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
});

module.exports = boundTrimLeft;
module.exports = bound;
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "npm run lint && npm run tests-only&& npm run security",
"test": "npm run lint && es-shim-api --bound && npm run tests-only && npm run security",
"tests-only": "npm run test:shimmed && npm run test:module",
"test:shimmed": "node test/shimmed.js",
"test:module": "node test/index.js",
Expand All @@ -29,7 +29,8 @@
"trim",
"trimLeft",
"trimRight",
"polyfill"
"polyfill",
"es-shim API"
],
"dependencies": {
"define-properties": "^1.1.2",
Expand All @@ -41,7 +42,8 @@
"jscs": "^2.9.0",
"nsp": "^2.2.0",
"eslint": "^1.10.3",
"@ljharb/eslint-config": "^1.6.1"
"@ljharb/eslint-config": "^1.6.1",
"@es-shims/api": "^1.1.0"
},
"testling": {
"files": "test/index.js",
Expand Down
14 changes: 14 additions & 0 deletions polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var implementation = require('./implementation');

module.exports = function getPolyfill() {
if (!String.prototype.trimLeft) {
return implementation;
}
var zeroWidthSpace = '\u200b';
if (zeroWidthSpace.trimLeft() !== zeroWidthSpace) {
return implementation;
}
return String.prototype.trimLeft;
};
14 changes: 14 additions & 0 deletions shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var define = require('define-properties');
var getPolyfill = require('./polyfill');

module.exports = function shimTrimLeft() {
var polyfill = getPolyfill();
define(
String.prototype,
{ trimLeft: polyfill },
{ trimLeft: function () { return String.prototype.trimLeft !== polyfill; } }
);
return polyfill;
};

0 comments on commit 0727d38

Please sign in to comment.