melody-runtime
Advanced tools
Comparing version 1.2.0-commit.644caa57 to 1.2.0-commit.66d6630f
@@ -13,2 +13,3 @@ import _isString from 'lodash/isString'; | ||
import _isNumber from 'lodash/isNumber'; | ||
import { enqueueComponent, link } from 'melody-idom'; | ||
@@ -763,2 +764,33 @@ /** | ||
function trim(str, charList) { | ||
var side = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'both'; | ||
if (charList === undefined && side === 'both') { | ||
// Use String.prototype.trim() for efficiency | ||
return String(str).trim(); | ||
} | ||
if (side !== 'both' && side !== 'left' && side !== 'right') { | ||
throw new Error('Filter "trim". Invalid value ' + side + ' for parameter "side". Valid values are "both", "left", "right".'); | ||
} | ||
var strLen = str.length; | ||
var trimStart = 0; | ||
if (side === 'both' || side === 'left') { | ||
while (trimStart < strLen && charList.indexOf(str[trimStart]) !== -1) { | ||
trimStart++; | ||
} | ||
} | ||
var trimEnd = strLen; | ||
if (side === 'both' || side === 'right') { | ||
while (trimEnd > 0 && charList.indexOf(str[trimEnd - 1]) !== -1) { | ||
trimEnd--; | ||
} | ||
} | ||
return str.substr(trimStart, trimEnd - trimStart); | ||
} | ||
var MAX_SAFE_INTEGER = 'MAX_SAFE_INTEGER' in Number ? Number.MAX_SAFE_INTEGER : 9007199254740991; | ||
@@ -864,2 +896,114 @@ | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var AsyncComponent = function () { | ||
function AsyncComponent() { | ||
classCallCheck(this, AsyncComponent); | ||
this.promisedComponent = null; | ||
this._el = null; | ||
this.refs = Object.create(null); | ||
this.props = null; | ||
this.resolvedComponent = null; | ||
this.alreadyUnmounted = false; | ||
this.isLoading = false; | ||
this.loadingError = null; | ||
} | ||
AsyncComponent.prototype.apply = function apply(props) { | ||
var _this = this; | ||
this.props = props; | ||
var promisedComponent = props.promisedComponent; | ||
// if we already have resolved the component, just pass the data forward | ||
if (this.resolvedComponent) { | ||
this.resolvedComponent.apply(this.props.data); | ||
} | ||
// otherwise | ||
if (!this.isLoading) { | ||
this.isLoading = true; | ||
promisedComponent().then(function (_ref) { | ||
var Component = _ref.default; | ||
if (_this.alreadyUnmounted) { | ||
return; | ||
} | ||
_this.resolvedComponent = new Component(); | ||
link(_this, _this.resolvedComponent); | ||
_this.resolvedComponent.el = _this._el; | ||
_this.resolvedComponent.apply(_this.props.data); | ||
}, function (err) { | ||
// fail early | ||
_this.loadingError = err; | ||
enqueueComponent(_this); | ||
}).catch(function (err) { | ||
// just in case something went wrong while initialising the component | ||
_this.loadingError = err; | ||
enqueueComponent(_this); | ||
}); | ||
if (this.props.delayLoadingAnimation) { | ||
setTimeout(function () { | ||
return enqueueComponent(_this); | ||
}, this.props.delayLoadingAnimation); | ||
} else { | ||
enqueueComponent(this); | ||
} | ||
} | ||
}; | ||
AsyncComponent.prototype.notify = function notify() {}; | ||
AsyncComponent.prototype.componentWillUnmount = function componentWillUnmount() { | ||
this.alreadyUnmounted = true; | ||
}; | ||
AsyncComponent.prototype.render = function render() { | ||
if (!this.resolvedComponent) { | ||
if (this.loadingError && this.props.onError) { | ||
this.props.onError(this.loadingError); | ||
} else { | ||
this.props.whileLoading(); | ||
} | ||
} | ||
}; | ||
createClass(AsyncComponent, [{ | ||
key: 'el', | ||
set: function set$$1(el) { | ||
if (this.resolvedComponent) { | ||
this.resolvedComponent.el = el; | ||
} | ||
this._el = el; | ||
return el; | ||
}, | ||
get: function get$$1() { | ||
return this.resolvedComponent ? this.resolvedComponent.el : this._el; | ||
} | ||
}]); | ||
return AsyncComponent; | ||
}(); | ||
/** | ||
@@ -881,2 +1025,2 @@ * Copyright 2017 trivago N.V. | ||
export { createSubContext, batch, attrs, styles, classes, merge, replace, reverse, round, striptags, number_format, format, title, url_encode, strtotime, random, min, max, cycle, attribute, isEmpty, inheritBlocks }; | ||
export { AsyncComponent, createSubContext, batch, attrs, styles, classes, merge, replace, reverse, round, striptags, number_format, format, title, url_encode, strtotime, trim, random, min, max, cycle, attribute, isEmpty, inheritBlocks }; |
146
lib/index.js
@@ -19,2 +19,3 @@ 'use strict'; | ||
var _isNumber = _interopDefault(require('lodash/isNumber')); | ||
var melodyIdom = require('melody-idom'); | ||
@@ -769,2 +770,33 @@ /** | ||
function trim(str, charList) { | ||
var side = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'both'; | ||
if (charList === undefined && side === 'both') { | ||
// Use String.prototype.trim() for efficiency | ||
return String(str).trim(); | ||
} | ||
if (side !== 'both' && side !== 'left' && side !== 'right') { | ||
throw new Error('Filter "trim". Invalid value ' + side + ' for parameter "side". Valid values are "both", "left", "right".'); | ||
} | ||
var strLen = str.length; | ||
var trimStart = 0; | ||
if (side === 'both' || side === 'left') { | ||
while (trimStart < strLen && charList.indexOf(str[trimStart]) !== -1) { | ||
trimStart++; | ||
} | ||
} | ||
var trimEnd = strLen; | ||
if (side === 'both' || side === 'right') { | ||
while (trimEnd > 0 && charList.indexOf(str[trimEnd - 1]) !== -1) { | ||
trimEnd--; | ||
} | ||
} | ||
return str.substr(trimStart, trimEnd - trimStart); | ||
} | ||
var MAX_SAFE_INTEGER = 'MAX_SAFE_INTEGER' in Number ? Number.MAX_SAFE_INTEGER : 9007199254740991; | ||
@@ -870,2 +902,114 @@ | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var AsyncComponent = function () { | ||
function AsyncComponent() { | ||
classCallCheck(this, AsyncComponent); | ||
this.promisedComponent = null; | ||
this._el = null; | ||
this.refs = Object.create(null); | ||
this.props = null; | ||
this.resolvedComponent = null; | ||
this.alreadyUnmounted = false; | ||
this.isLoading = false; | ||
this.loadingError = null; | ||
} | ||
AsyncComponent.prototype.apply = function apply(props) { | ||
var _this = this; | ||
this.props = props; | ||
var promisedComponent = props.promisedComponent; | ||
// if we already have resolved the component, just pass the data forward | ||
if (this.resolvedComponent) { | ||
this.resolvedComponent.apply(this.props.data); | ||
} | ||
// otherwise | ||
if (!this.isLoading) { | ||
this.isLoading = true; | ||
promisedComponent().then(function (_ref) { | ||
var Component = _ref.default; | ||
if (_this.alreadyUnmounted) { | ||
return; | ||
} | ||
_this.resolvedComponent = new Component(); | ||
melodyIdom.link(_this, _this.resolvedComponent); | ||
_this.resolvedComponent.el = _this._el; | ||
_this.resolvedComponent.apply(_this.props.data); | ||
}, function (err) { | ||
// fail early | ||
_this.loadingError = err; | ||
melodyIdom.enqueueComponent(_this); | ||
}).catch(function (err) { | ||
// just in case something went wrong while initialising the component | ||
_this.loadingError = err; | ||
melodyIdom.enqueueComponent(_this); | ||
}); | ||
if (this.props.delayLoadingAnimation) { | ||
setTimeout(function () { | ||
return melodyIdom.enqueueComponent(_this); | ||
}, this.props.delayLoadingAnimation); | ||
} else { | ||
melodyIdom.enqueueComponent(this); | ||
} | ||
} | ||
}; | ||
AsyncComponent.prototype.notify = function notify() {}; | ||
AsyncComponent.prototype.componentWillUnmount = function componentWillUnmount() { | ||
this.alreadyUnmounted = true; | ||
}; | ||
AsyncComponent.prototype.render = function render() { | ||
if (!this.resolvedComponent) { | ||
if (this.loadingError && this.props.onError) { | ||
this.props.onError(this.loadingError); | ||
} else { | ||
this.props.whileLoading(); | ||
} | ||
} | ||
}; | ||
createClass(AsyncComponent, [{ | ||
key: 'el', | ||
set: function set$$1(el) { | ||
if (this.resolvedComponent) { | ||
this.resolvedComponent.el = el; | ||
} | ||
this._el = el; | ||
return el; | ||
}, | ||
get: function get$$1() { | ||
return this.resolvedComponent ? this.resolvedComponent.el : this._el; | ||
} | ||
}]); | ||
return AsyncComponent; | ||
}(); | ||
/** | ||
@@ -887,2 +1031,3 @@ * Copyright 2017 trivago N.V. | ||
exports.AsyncComponent = AsyncComponent; | ||
exports.createSubContext = createSubContext; | ||
@@ -903,2 +1048,3 @@ exports.batch = batch; | ||
exports.strtotime = strtotime; | ||
exports.trim = trim; | ||
exports.random = random; | ||
@@ -905,0 +1051,0 @@ exports.min = min; |
{ | ||
"name": "melody-runtime", | ||
"version": "1.2.0-commit.644caa57", | ||
"version": "1.2.0-commit.66d6630f", | ||
"description": "", | ||
@@ -15,2 +15,5 @@ "main": "./lib/index.js", | ||
}, | ||
"peerDependencies": { | ||
"melody-idom": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
@@ -17,0 +20,0 @@ "rollup-plugin-babel": "^2.6.1" |
@@ -488,3 +488,3 @@ /** | ||
// Plain Math.round doesn't just truncate | ||
number = Math.round(number - number % 1); | ||
number = Math.round(number - (number % 1)); | ||
prefix = number < 0 ? '-' : positivePrefix; | ||
@@ -965,1 +965,34 @@ value = | ||
} | ||
export function trim(str, charList, side = 'both') { | ||
if (charList === undefined && side === 'both') { | ||
// Use String.prototype.trim() for efficiency | ||
return String(str).trim(); | ||
} | ||
if (side !== 'both' && side !== 'left' && side !== 'right') { | ||
throw new Error( | ||
'Filter "trim". Invalid value ' + | ||
side + | ||
' for parameter "side". Valid values are "both", "left", "right".' | ||
); | ||
} | ||
const strLen = str.length; | ||
let trimStart = 0; | ||
if (side === 'both' || side === 'left') { | ||
while (trimStart < strLen && charList.indexOf(str[trimStart]) !== -1) { | ||
trimStart++; | ||
} | ||
} | ||
let trimEnd = strLen; | ||
if (side === 'both' || side === 'right') { | ||
while (trimEnd > 0 && charList.indexOf(str[trimEnd - 1]) !== -1) { | ||
trimEnd--; | ||
} | ||
} | ||
return str.substr(trimStart, trimEnd - trimStart); | ||
} |
@@ -32,4 +32,8 @@ /** | ||
strtotime, | ||
trim, | ||
} from './filters'; | ||
export { random, min, max, cycle, attribute } from './functions'; | ||
export { isEmpty, inheritBlocks } from './helpers'; | ||
import AsyncComponent from './async'; | ||
export { AsyncComponent }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114279
9
3044
2