Skip to content
This repository has been archived by the owner on Feb 7, 2022. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Oct 4, 2020
0 parents commit 91b8c5e
Show file tree
Hide file tree
Showing 10 changed files with 1,502 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
37 changes: 37 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"env": {
"browser": true,
"es2020": true,
"node": true
},
"extends": [
"airbnb-base"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 11,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/no-unused-vars": [
"error"
],
"max-len": [
"error",
120
],
"no-restricted-globals": "off",
"no-restricted-syntax": "off",
"no-unused-vars": "off",
"prefer-rest-params": "off",
"no-param-reassign": "off"
},
"ignorePatterns": [
"dist/*",
"node_modules/*",
"_.js"
]
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Packages
node_modules/

# Log files
yarn-error.log

# Build directories
dist/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @almeidx/is-empty

Checks if value is an empty object, collection, map, or set.

Objects are considered empty if they have no own enumerable string keyed properties.

Array-like values such as arguments objects, arrays, buffers, strings, or jQuery-like collections are considered empty if they have a length of 0. Similarly, maps and sets are considered empty if they have a size of 0.

```js
import isEmpty from '@almeidx/is-empty';
// or
const isEmpty = require('@almeidx/is-empty');

isEmpty(null);
// => true

isEmpty(true);
// => true

isEmpty(1);
// => true

isEmpty([1, 2, 3]);
// => false

isEmpty({ 'a': 1 });
// => false
```
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@almeidx/is-empty",
"version": "1.0.0",
"description": "Tiny millisecond conversion utility",
"repository": "https://github.com/almeidx/is-empty.git",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"keywords": [
"is-empty",
"empty",
"length-check"
],
"author": "almeidx <almeidx@pm.me> (almeidx.me)",
"scripts": {
"build": "rimraf dist && tsc",
"lint": "eslint src --ext ts",
"lint:fix": "eslint src --ext ts --fix",
"prepublish": "rimraf dist && tsc"
},
"devDependencies": {
"@types/node": "^14.11.2",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"eslint": "^7.4.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-plugin-import": "2.21.2",
"rimraf": "^3.0.2",
"typescript": "^4.0.3"
},
"license": "MIT"
}
93 changes: 93 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as util from 'util';

const getRawTag = (value: any): string => {
const isOwn = Object.prototype.hasOwnProperty.call(value, Symbol.toStringTag);
const tag = value[Symbol.toStringTag];

let unmasked = false;
try {
value[Symbol.toStringTag] = undefined;
unmasked = true;
} catch {} // eslint-disable-line no-empty

const result = Object.prototype.toString.call(value);
if (unmasked) {
if (isOwn) {
value[Symbol.toStringTag] = tag;
} else {
delete value[Symbol.toStringTag];
}
}
return result;
};

const getTag = (value: any): string => {
if (value == null) {
return value === undefined ? '[object Undefined]' : '[object Null]';
}
return (Symbol.toStringTag in Object(value))
? getRawTag(value)
: Object.prototype.toString.call(value);
};

const isFunction = (value: any): boolean => {
if (value == null || (typeof value !== 'object' && typeof value !== 'function')) return false;

const tag = getTag(value);
return tag === '[object Function]'
|| tag === '[object GeneratorFunction]'
|| tag === '[object AsyncFunction]'
|| tag === '[object Proxy]';
};

function isPrototype(value: any) {
const valProto = value && value.constructor;
return value === (typeof valProto === 'function' && valProto.prototype) || Object.prototype;
}

const overArg = (
func: (...args: any[]) => any,
transform: any,
// eslint-disable-next-line function-paren-newline
): (...args: any[]) => any => function recursive(arg) {
return func(transform(arg));
};

const baseKeys = (object: any) => {
if (!isPrototype(object)) return overArg(Object.keys, Object)(object);

const result = [];
for (const key in Object(object)) {
if (Object.prototype.hasOwnProperty.call(object, key) && key !== 'constructor') result.push(key);
}
return result;
};

export const isEmpty = (value: any): boolean => {
if (value == null) return true;

if (
(value != null && typeof value.length === 'number' && !isFunction(value))
&& (
Array.isArray(value)
|| typeof value === 'string'
|| typeof value.splice === 'function'
|| Buffer.isBuffer(value)
|| util.types.isTypedArray(value)
|| (typeof value === 'object' && getTag(value) === '[object Arguments]')
)
) return !value.length;

const tag = getTag(value);
if (tag === '[object Map]' || tag === '[object Set]') return !value.size;

if (isPrototype(value)) return !baseKeys(value).length;

for (const key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) return false;
}

return true;
};

export default isEmpty;
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"noUnusedLocals": false,
"removeComments": true,
"module": "commonjs",
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"target": "es2019",
"rootDir": "src",
"outDir": "dist",
"declaration": true
},
"exclude": [
"node_modules",
"dist"
]
}
Loading

0 comments on commit 91b8c5e

Please sign in to comment.