Skip to content

Commit

Permalink
[version 2.0.0] Validate NaN and invalid dates as false 🦕
Browse files Browse the repository at this point in the history
Validate NaN and invalid dates as false 🦕
  • Loading branch information
mesqueeb authored Sep 10, 2018
2 parents b065363 + 91f9014 commit 376ec8e
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 255 deletions.
5 changes: 2 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"presets": [
["env", {
["@babel/preset-env", {
"modules": false
}]
],
"plugins": ["external-helpers"]
]
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function isString (payload) {
return getType(payload) === 'String'
}
function isNumber (payload) {
return getType(payload) === 'Number'
return (getType(payload) === 'Number' && !isNaN(payload))
}
function isBoolean (payload) {
return getType(payload) === 'Boolean'
Expand All @@ -34,10 +34,12 @@ function isRegExp (payload) {
return getType(payload) === 'RegExp'
}
function isDate (payload) {
return getType(payload) === 'Date'
return (getType(payload) === 'Date' && !isNaN(payload))
}
```

Since v2.0.0 it will return false on `isNumber()` and `isDate()` if the payload is `NaN` or an invalid date.

## Build from source

```bash
Expand Down
120 changes: 88 additions & 32 deletions build/rollup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
/* eslint-disable */

/* Required packages: */
// npm i -D \
// @babel/core \
// @babel/plugin-proposal-object-rest-spread \
// @babel/preset-env \
// rollup \
// rollup-plugin-babel@latest \
// rollup-plugin-commonjs \
// rollup-plugin-node-resolve \
// rollup-plugin-terser \
// is-what

/* Required .babelrc setup: */
// {
// "presets": [
// ["@babel/preset-env", {
// "modules": false
// }]
// ],
// "plugins": [
// "@babel/plugin-proposal-object-rest-spread"
// ]
// }

import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
import { terser } from 'rollup-plugin-terser'
import { isArray } from 'is-what'
// import resolve from 'rollup-plugin-node-resolve'

// ------------------------------------------------------------------------------------------
// formats
Expand All @@ -13,50 +41,78 @@ import { terser } from 'rollup-plugin-terser'
// ------------------------------------------------------------------------------------------
// setup
// ------------------------------------------------------------------------------------------
const files = [
{in: 'src/index.js', out: 'dist', formats: ['cjs', 'es', 'iife', 'umd']},
]
const minify = true
const sourcemap = true
const plugins = [
babel({
exclude: 'node_modules/**' // only transpile our source code
}),
commonjs()
]
// ------------------------------------------------------------------------------------------
const pkg = require('../package.json')
const name = pkg.name
const className = name.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())
const external = Object.keys(pkg.dependencies || [])
const _plugins = [
babel({
exclude: 'node_modules/**', // only transpile our source code
}),
commonjs(),
]

// ------------------------------------------------------------------------------------------
// build helpers
// Builds
// ------------------------------------------------------------------------------------------
function output (ext, format) {
const nsNameExt = new RegExp('(.+)\/([^\/]+)\.([^\.]+$)', 'g')
function getNS (name) {
if (!name.includes('/')) return ''
return name.replace(nsNameExt, '$1')
}
function getName (name) {
if (!name.includes('/')) name = 'a/' + name
name = name.replace(nsNameExt, '$2')
if (name.endsWith('.')) name = name.slice(0, -1)
return name
}
function getExt (name) {
return name.split('.').pop()
}
function getFileInfo (file) {
return {
name: className,
sourcemap: true,
exports: 'named',
file: `dist/index.${ext}`,
format
ns: getNS(file.in),
name: getName(file.in),
ext: getExt(file.in),
out: file.out,
formats: !isArray(file.formats) ? [file.formats] : file.formats,
plugins: (file.plugins === undefined) ? plugins : file.plugins,
min: (file.minify === undefined) ? minify : file.minify,
map: (file.sourcemap === undefined) ? sourcemap : file.sourcemap,
external: external,
}
}
function buildTemplate (format, minified = false) {
const plugins = (minified)
? _plugins.concat(terser())
: _plugins
const ext = (minified)
? `${format}.min.js`
: `${format}.js`
function getRollupObject (info, format) {
return {
input: `src/index.js`,
output: output(ext, format),
plugins,
external
input: `${info.ns}/${info.name}.${info.ext}`,
output: {
name: className,
sourcemap: info.map,
exports: 'named',
file: (!info.min)
? `${info.out}/${info.name}.${format}.${info.ext}`
: `${info.out}/${info.name}.${format}.min.${info.ext}`,
format
},
plugins: (!info.min) ? plugins : plugins.concat(terser()),
external: info.external
}
}
const builds = files.reduce((carry, file) => {
const info = getFileInfo(file)
const _builds = info.formats
.reduce((carry, format) => {
return carry
.concat(getRollupObject(info, format))
}, [])

// ------------------------------------------------------------------------------------------
// builds
// ------------------------------------------------------------------------------------------
const formats = ['umd', 'cjs', 'es', 'iife']
const regBuilds = formats.map(format => buildTemplate(format))
const minBuilds = formats.map(format => buildTemplate(format, true))
const result = regBuilds.concat(minBuilds)
return carry.concat(_builds)
}, [])

export default result
export default builds
50 changes: 0 additions & 50 deletions dist/index.cjs.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/index.cjs.js.map

This file was deleted.

2 changes: 1 addition & 1 deletion dist/index.cjs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.cjs.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 0 additions & 36 deletions dist/index.es.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/index.es.js.map

This file was deleted.

2 changes: 1 addition & 1 deletion dist/index.es.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 376ec8e

Please sign in to comment.