Created
November 6, 2017 16:15
-
-
Save ordoghl/09ff94a1965be9aab6a8c9548bb272fc to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/fofure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
var NODATA_VALUE = '#nodata#'; | |
function numberType() { | |
return function (value) { | |
if (typeof value !== 'number') { | |
return 'not a number'; | |
} | |
}; | |
} | |
function numberValue(_ref) { | |
var min = _ref.min; | |
var max = _ref.max; | |
return function (value) { | |
if (value && min && min > value) { | |
return 'must be at least ' + min; | |
} | |
if (value && min && max < value) { | |
return 'must be at most ' + max; | |
} | |
}; | |
} | |
function noDataWrapper(validator) { | |
return function () { | |
for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) { | |
params[_key] = arguments[_key]; | |
} | |
return function (value) { | |
if (value === NODATA_VALUE) { | |
return undefined; | |
} | |
for (var _len2 = arguments.length, others = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | |
others[_key2 - 1] = arguments[_key2]; | |
} | |
return validator.apply(undefined, params).apply(undefined, [value].concat(others)); | |
}; | |
}; | |
} | |
var nt = noDataWrapper(numberType); | |
var nv = noDataWrapper(numberValue); | |
console.clear(); | |
console.log(nt()(3)); | |
console.log(nt()('alma')); | |
console.log(nt()('#nodata#')); | |
console.log(nv({ min: 2 })(1)); | |
console.log(nv({ min: 2 })('#nodata#')); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const NODATA_VALUE = '#nodata#' | |
function numberType() { | |
return function(value) { | |
if (typeof value !== 'number') { | |
return 'not a number' | |
} | |
} | |
} | |
function numberValue({ min, max }) { | |
return function(value) { | |
if (value && min && min > value) { | |
return `must be at least ${min}` | |
} | |
if (value && min && max < value) { | |
return `must be at most ${max}` | |
} | |
} | |
} | |
function noDataWrapper(validator) { | |
return function(...params) { | |
return function(value, ...others) { | |
if (value === NODATA_VALUE) { | |
return undefined | |
} | |
return validator(...params)(value, ...others) | |
} | |
} | |
} | |
const nt = noDataWrapper(numberType) | |
const nv = noDataWrapper(numberValue) | |
console.clear() | |
console.log(nt()(3)) | |
console.log(nt()('alma')) | |
console.log(nt()('#nodata#')) | |
console.log(nv({min: 2})(1)) | |
console.log(nv({min: 2})('#nodata#'))</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var NODATA_VALUE = '#nodata#'; | |
function numberType() { | |
return function (value) { | |
if (typeof value !== 'number') { | |
return 'not a number'; | |
} | |
}; | |
} | |
function numberValue(_ref) { | |
var min = _ref.min; | |
var max = _ref.max; | |
return function (value) { | |
if (value && min && min > value) { | |
return 'must be at least ' + min; | |
} | |
if (value && min && max < value) { | |
return 'must be at most ' + max; | |
} | |
}; | |
} | |
function noDataWrapper(validator) { | |
return function () { | |
for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) { | |
params[_key] = arguments[_key]; | |
} | |
return function (value) { | |
if (value === NODATA_VALUE) { | |
return undefined; | |
} | |
for (var _len2 = arguments.length, others = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | |
others[_key2 - 1] = arguments[_key2]; | |
} | |
return validator.apply(undefined, params).apply(undefined, [value].concat(others)); | |
}; | |
}; | |
} | |
var nt = noDataWrapper(numberType); | |
var nv = noDataWrapper(numberValue); | |
console.clear(); | |
console.log(nt()(3)); | |
console.log(nt()('alma')); | |
console.log(nt()('#nodata#')); | |
console.log(nv({ min: 2 })(1)); | |
console.log(nv({ min: 2 })('#nodata#')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment