diff --git a/CHANGELOG.md b/CHANGELOG.md index d3b1bf4..6c2f52d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,14 @@ by [**SWR Audio Lab**](https://lab.swr.de/) ## Changelog +- 2022-01-31 - v1.0.1-beta + - feat: add string helpers + - 2022-01-26 - v1.0.0-beta - chore!: folder migration - refactor: swap node-fetch for undici - feat: add ard publisher script - feat: add ard category parser - - feat: add string helpers - 2021-08-11 - v0.2.3 - updated dependencies diff --git a/package.json b/package.json index 47bfd84..f59261a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@swrlab/utils", - "version": "1.0.0-beta", + "version": "1.0.1-beta", "description": "Wrapping common SWR Audio Lab utils", "main": "./src/index.js", "engines": { @@ -21,7 +21,7 @@ "private": false, "dependencies": { "@google-cloud/storage": "5.18.0", - "aws-sdk": "2.1062.0", + "aws-sdk": "2.1063.0", "node-crc": "^1.3.2", "undici-wrapper": "frytg/undici-wrapper#v0.0.3", "uuid": "8.3.2" @@ -29,7 +29,7 @@ "devDependencies": { "@swrlab/eslint-plugin-swr": "0.1.2", "@swrlab/swr-prettier-config": "0.1.2", - "dotenv": "^14.2.0", + "dotenv": "^14.3.2", "eslint": "8.7.0", "prettier": "^2.5.1" }, diff --git a/packages/strings/README.md b/packages/strings/README.md index fac61d5..5fbef5f 100644 --- a/packages/strings/README.md +++ b/packages/strings/README.md @@ -4,9 +4,17 @@ Common string, array, object encoding and getter helpers. - [SWR Audio Lab / Strings, Arrays, Objects](#swr-audio-lab--strings-arrays-objects) - [Install](#install) - - [`isArray` - Check if a value is a proper array](#isarray---check-if-a-value-is-a-proper-array) - - [`isIncluded` - Check if a value (haystack) includes another value (needle)](#isincluded---check-if-a-value-haystack-includes-another-value-needle) + - [`getObjectLength` - get the length of an object](#getobjectlength---get-the-length-of-an-object) + - [`isArray` - check if a value is a proper array](#isarray---check-if-a-value-is-a-proper-array) + - [`isEmptyArray` - check if a value is an empty array](#isemptyarray---check-if-a-value-is-an-empty-array) + - [`isEmptyObject` - check if a value is an empty object](#isemptyobject---check-if-a-value-is-an-empty-object) + - [`isIncluded` - check if a value (haystack) includes another value (needle)](#isincluded---check-if-a-value-haystack-includes-another-value-needle) + - [`isNull` - check if a value is null](#isnull---check-if-a-value-is-null) - [`isObject` - check if a value is a proper object](#isobject---check-if-a-value-is-a-proper-object) + - [`isUndefined` - check if a value is undefined](#isundefined---check-if-a-value-is-undefined) + - [`notEmptyArray` - check if a value is not an empty array](#notemptyarray---check-if-a-value-is-not-an-empty-array) + - [`notEmptyObject` - check if a value is not an empty object](#notemptyobject---check-if-a-value-is-not-an-empty-object) + - [`notNullOrUndefined` - check if a value is neither null nor undefined](#notnullorundefined---check-if-a-value-is-neither-null-nor-undefined) - [`removeDoubleSpaces` - take a string and remove its duplicate spaces](#removedoublespaces---take-a-string-and-remove-its-duplicate-spaces) - [`toHex` - take a string convert it to a hex string](#tohex---take-a-string-convert-it-to-a-hex-string) @@ -18,7 +26,27 @@ Add the parent package to your dependencies: yarn add @swrlab/utils ``` -## `isArray` - Check if a value is a proper array +## `getObjectLength` - get the length of an object + +- `value` (required) - Value to check + +Import the library: + +```js +const { getObjectLength } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +getObjectLength({ hello: 'world' }) +// 1 + +getObjectLength({ hello: 'world', foo: 'bar' }) +// 2 +``` + +## `isArray` - check if a value is a proper array - `value` (required) - Value to check @@ -38,7 +66,47 @@ isArray('hello world') // false ``` -## `isIncluded` - Check if a value (haystack) includes another value (needle) +## `isEmptyArray` - check if a value is an empty array + +- `value` (required) - Value to check + +Import the library: + +```js +const { isEmptyArray } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +isEmptyArray([]) +// true + +isEmptyArray(['hello world']) +// false +``` + +## `isEmptyObject` - check if a value is an empty object + +- `value` (required) - Value to check + +Import the library: + +```js +const { isEmptyObject } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +isEmptyObject({}) +// true + +isEmptyObject({ hello: 'world' }) +// false +``` + +## `isIncluded` - check if a value (haystack) includes another value (needle) - `haystack` (required) - Array or value to check - `needle` (required) - Array or value to check @@ -59,6 +127,26 @@ isIncluded('hello world', 'earth') // false ``` +## `isNull` - check if a value is null + +- `value` (required) - Value to check + +Import the library: + +```js +const { isNull } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +isNull(null) +// true + +isNull(undefined) +// false +``` + ## `isObject` - check if a value is a proper object - `value` (required) - Value to check @@ -79,6 +167,89 @@ isObject('hello world') // false ``` +## `isUndefined` - check if a value is undefined + +- `value` (required) - Value to check + +Import the library: + +```js +const { isUndefined } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +isUndefined(undefined) +// true + +isUndefined(null) +// false +``` + +## `notEmptyArray` - check if a value is not an empty array + +- `value` (required) - Value to check + +Import the library: + +```js +const { notEmptyArray } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +notEmptyArray(['hello world']) +// true + +notEmptyArray([]) +// false +``` + +## `notEmptyObject` - check if a value is not an empty object + +- `value` (required) - Value to check + +Import the library: + +```js +const { notEmptyObject } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +notEmptyObject({ hello: 'world' }) +// true + +notEmptyObject({}) +// false +``` + +## `notNullOrUndefined` - check if a value is neither null nor undefined + +- `value` (required) - Value to check + +Import the library: + +```js +const { notNullOrUndefined } = require('@swrlab/utils/packages/strings') +``` + +Then use the toolkit: + +```js +notNullOrUndefined('hello world') +// true + +notNullOrUndefined(null) +// false + +notNullOrUndefined(undefined) +// false +``` + ## `removeDoubleSpaces` - take a string and remove its duplicate spaces - `value` (required) - Value to convert diff --git a/packages/strings/index.js b/packages/strings/index.js index 7e214e9..c832183 100644 --- a/packages/strings/index.js +++ b/packages/strings/index.js @@ -1,15 +1,31 @@ // import packages +const getObjectLength = require('../../utils/strings/getObjectLength') const isArray = require('../../utils/strings/isArray') +const isEmptyArray = require('../../utils/strings/isEmptyArray') +const isEmptyObject = require('../../utils/strings/isEmptyObject') const isIncluded = require('../../utils/strings/isIncluded') +const isNull = require('../../utils/strings/isNull') const isObject = require('../../utils/strings/isObject') +const isUndefined = require('../../utils/strings/isUndefined') +const notEmptyArray = require('../../utils/strings/notEmptyArray') +const notEmptyObject = require('../../utils/strings/notEmptyObject') +const notNullOrUndefined = require('../../utils/strings/notNullOrUndefined') const removeDoubleSpaces = require('../../utils/strings/removeDoubleSpaces') const toHex = require('../../utils/strings/toHex') // export packages module.exports = { + getObjectLength, isArray, + isEmptyArray, + isEmptyObject, isIncluded, + isNull, isObject, + isUndefined, + notEmptyArray, + notEmptyObject, + notNullOrUndefined, removeDoubleSpaces, toHex, } diff --git a/utils/strings/getObjectLength.js b/utils/strings/getObjectLength.js new file mode 100644 index 0000000..fdf73d7 --- /dev/null +++ b/utils/strings/getObjectLength.js @@ -0,0 +1,2 @@ +// get the length (count of keys) of an object +module.exports = (value) => value && Object.keys(value).length diff --git a/utils/strings/isArray.js b/utils/strings/isArray.js index 3a05411..4ea4453 100644 --- a/utils/strings/isArray.js +++ b/utils/strings/isArray.js @@ -1,2 +1,4 @@ +const notNullOrUndefined = require('./notNullOrUndefined') + // check if a variable is really an array -module.exports = (value) => !!(value instanceof Array) +module.exports = (value) => notNullOrUndefined(value) && value instanceof Array diff --git a/utils/strings/isEmptyArray.js b/utils/strings/isEmptyArray.js new file mode 100644 index 0000000..6595737 --- /dev/null +++ b/utils/strings/isEmptyArray.js @@ -0,0 +1,4 @@ +const isArray = require('./isArray') + +// check if a variable is an empty array +module.exports = (value) => isArray(value) && value.length === 0 diff --git a/utils/strings/isEmptyObject.js b/utils/strings/isEmptyObject.js new file mode 100644 index 0000000..cc4ec28 --- /dev/null +++ b/utils/strings/isEmptyObject.js @@ -0,0 +1,5 @@ +const isObject = require('./isObject') +const getObjectLength = require('./getObjectLength') + +// check if a variable is an empty object +module.exports = (value) => isObject(value) && getObjectLength(value) === 0 diff --git a/utils/strings/isNull.js b/utils/strings/isNull.js new file mode 100644 index 0000000..9e32efe --- /dev/null +++ b/utils/strings/isNull.js @@ -0,0 +1,2 @@ +// check if a variable is null +module.exports = (value) => value === null diff --git a/utils/strings/isObject.js b/utils/strings/isObject.js index c3c26d5..086ace1 100644 --- a/utils/strings/isObject.js +++ b/utils/strings/isObject.js @@ -1,5 +1,6 @@ // import utils const isArray = require('./isArray') +const notNullOrUndefined = require('./notNullOrUndefined') // check if a variable is really an object -module.exports = (value) => value instanceof Object && !isArray(value) +module.exports = (value) => notNullOrUndefined(value) && value instanceof Object && !isArray(value) diff --git a/utils/strings/isUndefined.js b/utils/strings/isUndefined.js new file mode 100644 index 0000000..ff200a6 --- /dev/null +++ b/utils/strings/isUndefined.js @@ -0,0 +1,2 @@ +// check if a variable is undefined +module.exports = (value) => value === undefined diff --git a/utils/strings/notEmptyArray.js b/utils/strings/notEmptyArray.js new file mode 100644 index 0000000..d4b88b6 --- /dev/null +++ b/utils/strings/notEmptyArray.js @@ -0,0 +1,4 @@ +const isArray = require('./isArray') + +// check if a variable is an empty array +module.exports = (value) => isArray(value) && value.length > 0 diff --git a/utils/strings/notEmptyObject.js b/utils/strings/notEmptyObject.js new file mode 100644 index 0000000..803a15e --- /dev/null +++ b/utils/strings/notEmptyObject.js @@ -0,0 +1,5 @@ +const isObject = require('./isObject') +const getObjectLength = require('./getObjectLength') + +// check if a variable is an empty object +module.exports = (value) => isObject(value) && getObjectLength(value) > 0 diff --git a/utils/strings/notNullOrUndefined.js b/utils/strings/notNullOrUndefined.js new file mode 100644 index 0000000..44e45aa --- /dev/null +++ b/utils/strings/notNullOrUndefined.js @@ -0,0 +1,5 @@ +const isNull = require('./isNull') +const isUndefined = require('./isUndefined') + +// check if a variable is neither null nor undefined +module.exports = (value) => !isNull(value) && !isUndefined(value) diff --git a/yarn.lock b/yarn.lock index e5f7f08..f193d94 100644 --- a/yarn.lock +++ b/yarn.lock @@ -418,10 +418,10 @@ async-retry@^1.3.3: dependencies: retry "0.13.1" -aws-sdk@2.1062.0: - version "2.1062.0" - resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.1062.0.tgz#89b55c9dcfa15135910a217489eb8577b11d9899" - integrity sha512-QIU8jwi7Uqyvw2HjsXXXUZv3V/6TinUzLewrdl2EdvonqZCXhwMgnZx2F9I2x62IKH1RqnINwFWdoK+OTgcAjA== +aws-sdk@2.1063.0: + version "2.1063.0" + resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.1063.0.tgz#ab5e7f69955358a48be345ee3d76667a68f61dd6" + integrity sha512-UonfKdsDChKEmAkFuDOQ8zeilvR5v7d5dEcWDy+fnKBs+6HGjDThMf7EofhOiKxOXWnFhrAsFKCsKDcfeA6NBg== dependencies: buffer "4.9.2" events "1.1.1" @@ -673,10 +673,10 @@ dot-prop@^5.2.0: dependencies: is-obj "^2.0.0" -dotenv@^14.2.0: - version "14.3.0" - resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-14.3.0.tgz#40f537fe90e229d35361c66cf432903e0db49001" - integrity sha512-PCTcOQSXVo9FI1dB7AichJXMEvmiGCq0gnCpjfDUc8505uR+2MeLXWe+Ue4PN5UXa2isHSa78sr7L59fk+2mnQ== +dotenv@^14.3.2: + version "14.3.2" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-14.3.2.tgz#7c30b3a5f777c79a3429cb2db358eef6751e8369" + integrity sha512-vwEppIphpFdvaMCaHfCEv9IgwcxMljMw2TnAQBB4VWPvzXQLTb82jwmdOKzlEVUL3gNFT4l4TPKO+Bn+sqcrVQ== duplexify@^4.0.0, duplexify@^4.1.1: version "4.1.2"