JSPM

  • Created
  • Published
  • Downloads 7358
  • Score
    100M100P100Q132111F
  • 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

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

Minimum Node version required Repository is on BitBucket Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

Table of Contents

Install

npm i util-nonempty
// consume as a CommonJS require:
const nonEmpty = require("util-nonempty");
// or as ES module:
import nonEmpty from "util-nonempty";

// then call as a function, pass it anything:
console.log(nonEmpty("a"));

Here's what you'll get:

Type Key in package.json Path Size
Main export - CommonJS version, transpiled to ES5, contains require and module.exports main dist/util-nonempty.cjs.js 756 B
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. module dist/util-nonempty.esm.js 603 B
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/util-nonempty.umd.js 929 B

⬆ back to top

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([123]);
// => 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 ast-is-empty
  • If your "emptiness" definition is "everything that String.trim()'s to an empty string'" (this includes tabs, spaces and line breaks for example, but not letters), see ast-contains-only-empty-space.

⬆ back to top

API

Anything-in, Boolean-out.

Contributing

  • If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.

  • If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.

  • If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.

⬆ back to top

Licence

MIT License (MIT)

Copyright © 2018 Codsen Ltd, Roy Revelt