Make polymer-expressions/path-parser a simple CommonJS module.

The weird module.exports code broke the webpack parser.
This commit is contained in:
Hristo Deshev
2015-12-02 18:47:05 +02:00
parent 643f9bf901
commit fa264d9374

View File

@ -7,10 +7,9 @@
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/ */
(function (global) { 'use strict';
'use strict';
function detectEval() { function detectEval() {
// Don't test for eval if we're running in a Chrome App environment. // Don't test for eval if we're running in a Chrome App environment.
// We check for APIs set that only exist in a Chrome App context. // We check for APIs set that only exist in a Chrome App context.
if (typeof chrome !== 'undefined' && chrome.app && chrome.app.runtime) { if (typeof chrome !== 'undefined' && chrome.app && chrome.app.runtime) {
@ -30,36 +29,36 @@
} catch (ex) { } catch (ex) {
return false; return false;
} }
} }
var hasEval = detectEval(); var hasEval = detectEval();
function isIndex(s) { function isIndex(s) {
return +s === s >>> 0 && s !== ''; return +s === s >>> 0 && s !== '';
} }
function toNumber(s) { function toNumber(s) {
return +s; return +s;
} }
function isObject(obj) { function isObject(obj) {
return obj === Object(obj); return obj === Object(obj);
} }
var numberIsNaN = global.Number.isNaN || function (value) { var numberIsNaN = Number.isNaN || function (value) {
return typeof value === 'number' && global.isNaN(value); return typeof value === 'number' && isNaN(value);
} }
function areSameValue(left, right) { function areSameValue(left, right) {
if (left === right) if (left === right)
return left !== 0 || 1 / left === 1 / right; return left !== 0 || 1 / left === 1 / right;
if (numberIsNaN(left) && numberIsNaN(right)) if (numberIsNaN(left) && numberIsNaN(right))
return true; return true;
return left !== left && right !== right; return left !== left && right !== right;
} }
var createObject = ('__proto__' in {}) ? var createObject = ('__proto__' in {}) ?
function (obj) { return obj; } : function (obj) { return obj; } :
function (obj) { function (obj) {
var proto = obj.__proto__; var proto = obj.__proto__;
@ -73,11 +72,11 @@
return newObject; return newObject;
}; };
var identStart = '[\$_a-zA-Z]'; var identStart = '[\$_a-zA-Z]';
var identPart = '[\$_a-zA-Z0-9]'; var identPart = '[\$_a-zA-Z0-9]';
var identRegExp = new RegExp('^' + identStart + '+' + identPart + '*' + '$'); var identRegExp = new RegExp('^' + identStart + '+' + identPart + '*' + '$');
function getPathCharType(char) { function getPathCharType(char) {
if (char === undefined) if (char === undefined)
return 'eof'; return 'eof';
@ -116,9 +115,9 @@
return 'number'; return 'number';
return 'else'; return 'else';
} }
var pathStateMachine = { var pathStateMachine = {
'beforePath': { 'beforePath': {
'ws': ['beforePath'], 'ws': ['beforePath'],
'ident': ['inIdent', 'append'], 'ident': ['inIdent', 'append'],
@ -184,11 +183,11 @@
'ws': ['afterElement'], 'ws': ['afterElement'],
']': ['inPath', 'push'] ']': ['inPath', 'push']
} }
} }
function noop() { } function noop() { }
function parsePath(path) { function parsePath(path) {
var keys = []; var keys = [];
var index = -1; var index = -1;
var c, newChar, key, type, transition, action, typeMap, mode = 'beforePath'; var c, newChar, key, type, transition, action, typeMap, mode = 'beforePath';
@ -249,15 +248,15 @@
} }
return; // parse error return; // parse error
} }
function isIdent(s) { function isIdent(s) {
return identRegExp.test(s); return identRegExp.test(s);
} }
var constructorIsPrivate = {}; var constructorIsPrivate = {};
function Path(parts, privateToken) { function Path(parts, privateToken) {
if (privateToken !== constructorIsPrivate) if (privateToken !== constructorIsPrivate)
throw Error('Use Path.get to retrieve path objects'); throw Error('Use Path.get to retrieve path objects');
@ -268,12 +267,12 @@
if (hasEval && this.length) { if (hasEval && this.length) {
this.getValueFrom = this.compiledGetValueFromFn(); this.getValueFrom = this.compiledGetValueFromFn();
} }
} }
// TODO(rafaelw): Make simple LRU cache // TODO(rafaelw): Make simple LRU cache
var pathCache = {}; var pathCache = {};
function getPath(pathString) { function getPath(pathString) {
if (pathString instanceof Path) if (pathString instanceof Path)
return pathString; return pathString;
@ -300,19 +299,19 @@
var path = new Path(parts, constructorIsPrivate); var path = new Path(parts, constructorIsPrivate);
pathCache[pathString] = path; pathCache[pathString] = path;
return path; return path;
} }
Path.get = getPath; Path.get = getPath;
function formatAccessor(key) { function formatAccessor(key) {
if (isIndex(key)) { if (isIndex(key)) {
return '[' + key + ']'; return '[' + key + ']';
} else { } else {
return '["' + key.replace(/"/g, '\\"') + '"]'; return '["' + key.replace(/"/g, '\\"') + '"]';
} }
} }
Path.prototype = createObject({ Path.prototype = createObject({
__proto__: [], __proto__: [],
valid: true, valid: true,
@ -385,25 +384,10 @@
obj[this[i]] = value; obj[this[i]] = value;
return true; return true;
} }
}); });
var invalidPath = new Path('', constructorIsPrivate); var invalidPath = new Path('', constructorIsPrivate);
invalidPath.valid = false; invalidPath.valid = false;
invalidPath.getValueFrom = invalidPath.setValueFrom = function () { }; invalidPath.getValueFrom = invalidPath.setValueFrom = function () { };
// Export the observe-js object for **Node.js**, with exports.Path = Path;
// backwards-compatibility for the old `require()` API. If we're in
// the browser, export as a global object.
var expose = global;
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
expose = exports = module.exports;
}
expose = exports;
}
expose.Path = Path;
})(typeof global !== 'undefined' && global && typeof module !== 'undefined' && module ? global : this || window);