JSPM

  • Created
  • Published
  • Downloads 7237
  • Score
    100M100P100Q131605F
  • License MIT

Is the input (plain object, array, string or whatever) not empty?

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

Standard JavaScript

Is the input (plain object, array, string or whatever) not empty?

Build Status Coverage Status bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies Downloads/Month

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.

Install

$ npm install --save util-nonempty

Use

var nonEmpty = require('util-nonempty')
console.log(nonEmpty('a'))

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

$ npm test

Unit tests use AVA and JS Standard notation. I aim to have 100% unit test coverage.

Contributing

All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js with new unit tests covering your feature(s).

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 merged in a timely manner. If you have any comments on the code, including ideas how to improve things, don't hesitate to contact me by email.

Licence

MIT License (MIT)

Copyright (c) 2017 Code and Send Ltd, Roy Reveltas

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.