Skip to content

Commit

Permalink
Merge pull request #342 from swrlab/dev/string-utils-update
Browse files Browse the repository at this point in the history
Add more string utils
  • Loading branch information
rafaelmaeuer authored Jan 31, 2022
2 parents 46d05bd + bb5d470 commit e9a6267
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 18 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -21,15 +21,15 @@
"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"
},
"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"
},
Expand Down
179 changes: 175 additions & 4 deletions packages/strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions packages/strings/index.js
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 2 additions & 0 deletions utils/strings/getObjectLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// get the length (count of keys) of an object
module.exports = (value) => value && Object.keys(value).length
4 changes: 3 additions & 1 deletion utils/strings/isArray.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions utils/strings/isEmptyArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const isArray = require('./isArray')

// check if a variable is an empty array
module.exports = (value) => isArray(value) && value.length === 0
5 changes: 5 additions & 0 deletions utils/strings/isEmptyObject.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions utils/strings/isNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// check if a variable is null
module.exports = (value) => value === null
3 changes: 2 additions & 1 deletion utils/strings/isObject.js
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions utils/strings/isUndefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// check if a variable is undefined
module.exports = (value) => value === undefined
4 changes: 4 additions & 0 deletions utils/strings/notEmptyArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const isArray = require('./isArray')

// check if a variable is an empty array
module.exports = (value) => isArray(value) && value.length > 0
5 changes: 5 additions & 0 deletions utils/strings/notEmptyObject.js
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions utils/strings/notNullOrUndefined.js
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e9a6267

Please sign in to comment.