Skip to content

Commit

Permalink
Merge pull request #1 from ewerk/feature/has-empty-value
Browse files Browse the repository at this point in the history
Feature/has empty value
  • Loading branch information
iceteabottle authored Sep 13, 2024
2 parents 91fe9ce + 3f8c70f commit a9c58c0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/pr-verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: PR Verification

on:
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run lint
- run: npm test
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ const titleNew$: string = value$.pipe(
);
```

### `hasEmptyValue`

This function checks if a value is considered "empty". A value is considered empty if it is an empty array, `null`, `undefined`, or an empty string.
This function could be useful for validating user input.

```typescript
import { hasEmptyValue } from '@ewerk/null-or-undefined';

console.log(hasEmptyValue('')); // true
console.log(hasEmptyValue([])); // true
console.log(hasEmptyValue(null)); // true
console.log(hasEmptyValue(undefined)); // true

console.log(hasEmptyValue(false)); // false
console.log(hasEmptyValue(0)); // false
console.log(hasEmptyValue('test string')); // false
console.log(hasEmptyValue(['test', 'string'])); // false
```

## Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Expand Down
20 changes: 20 additions & 0 deletions src/has-empty-value/has-empty-value.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { hasEmptyValue } from './has-empty-value';

describe('hasEmptyValue helper', () => {
it('should return true, if "empty" value was provided', () => {
expect(hasEmptyValue('')).toBe(true);
expect(hasEmptyValue([])).toBe(true);
expect(hasEmptyValue(null as unknown as string)).toBe(true);
expect(hasEmptyValue(undefined as unknown as string)).toBe(true);
});

it('should return false, if "non-empty" value was provided', () => {
expect(hasEmptyValue(false)).toBe(false);
expect(hasEmptyValue(0)).toBe(false);
expect(hasEmptyValue('test string')).toBe(false);
expect(hasEmptyValue([
'test',
'string',
])).toBe(false);
});
});
8 changes: 8 additions & 0 deletions src/has-empty-value/has-empty-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { isNullOrUndefined } from '../is-null-or-undefined/is-null-or-undefined';

export const hasEmptyValue = (val: number | string | boolean | Array<any>): boolean => {
if (val instanceof Array) {
return val.length === 0;
}
return isNullOrUndefined(val) || val === '' ;
};

0 comments on commit a9c58c0

Please sign in to comment.