Package Exports
- util-nonempty
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (util-nonempty) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
util-nonempty
Is the input (plain object, array, string or whatever) not empty?
Table of Contents
Install
$ npm install --save util-nonempty
You'll get a transpiled code, served from index-es5.js
. The original (index.js
) is in ES6.
Use
var nonEmpty = require('util-nonempty')
console.log(nonEmpty('a'))
Purpose
I want a quick utility function, to be able to detect is the input not empty.
nonEmpty('z')
// => true
nonEmpty('')
// => false
nonEmpty(['a'])
// => true
nonEmpty([])
// => false
nonEmpty({a: 'a'})
// => true
nonEmpty({})
// => false
var f = function () { return 'z' }
nonEmpty(f)
// => false (answer is instantly false if input is not array, plain object or string)
If you want to check non-emptiness of complex nested trees of objects, arrays and strings (like parsed HTML AST), you need a library which can recursively traverse that. There are two options:
- If you want to check for strict emptiness, that is
[]
or{}
is empty, but{aaa: ' \n\n\n ', ' \t'}
is not, see posthtml-ast-is-empty - If your "emptiness" definition is wider — anything (plain object, array or string or a mix of thereof) that contains only whitespace (spaces, line breaks, tabs and so on), see posthtml-ast-contains-only-empty-space.
API
nonEmpty (
input // Array, plain object or string will be checked, rest will be instantly "false", unless input's missing (then returns undefined).
);
// => Boolean|undefined
Testing and Contributing
$ npm test
If you want to contribute, don't hesitate. If it's a code contribution, please supplement test.js
with tests covering your code. This library uses airbnb-base
rules preset of eslint
with few exceptions^ and follows the Semver rules.
If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to help you to get it quickly. If you have any comments on the code, including ideas how to improve things, just email me.
^ 1. No semicolons. 2. Allow plus-plus in for
loops. See ./eslintrc
Licence
MIT License (MIT)
Copyright (c) 2017 Codsen Ltd, Roy Revelt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.