Skip to content

Commit

Permalink
Merge pull request #8 from productive-codebases/fix/filterFalsies
Browse files Browse the repository at this point in the history
Fix filterFalsies.
  • Loading branch information
cr0cK authored Mar 7, 2024
2 parents 1b67331 + 67fb135 commit fc46b6d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.1.4

### Fixed

- Fix `filterFalsies` which was reversed.

## v1.1.3

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@productive-codebases/toolbox",
"description": "Tooling for productive codebases.",
"version": "1.1.3",
"version": "1.1.4",
"scripts": {
":d": "npm run dev",
"build": "tsc && vite build",
Expand Down
24 changes: 24 additions & 0 deletions src/libs/filterFalsies/filterFalsies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { filterFalsies } from '.'

describe('filterFalsies', () => {
it('should remove falsies values', () => {
const arr = [
0,
1,
'two',
undefined,
'four',
5,
null,
'',
'height',
{},
false,
NaN
]

const results = filterFalsies(arr)

expect(results).toEqual([1, 'two', 'four', 5, 'height', {}, NaN])
})
})
10 changes: 4 additions & 6 deletions src/libs/filterFalsies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import { isTruthy } from '../isTruthy'
* Usage:
* arr.filter(isNotFalsy)
*/
export function isNotFalsy<T>(o: Perhaps<T | boolean | string>): o is T {
export function isNotFalsy<T>(o: Perhaps<T>): o is T {
return !isTruthy(o)
}

/**
* Remove from an array falsy values.
* Remove falsy values of an array.
*/
export function filterFalsies<T>(
arr: Array<T | null | undefined | boolean>
): T[] {
return arr.filter(isNotFalsy)
export function filterFalsies<T>(arr: Array<T>): T[] {
return arr.filter(isTruthy)
}
15 changes: 4 additions & 11 deletions src/libs/isDefined/isDefined.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ import { isDefined } from '.'

describe('isDefined', () => {
it('should remove undefined or nullable values', () => {
const arr = [0, 1, 'two', undefined, 'four', 5, null, '', 'height', NaN]
const arr = [0, 1, 'two', undefined, 'four', 5, null, '', 'height', {}, NaN]

expect(arr.filter(isDefined)).toEqual([
0,
1,
'two',
'four',
5,
'',
'height',
NaN
])
const results = arr.filter(isDefined)

expect(results).toEqual([0, 1, 'two', 'four', 5, '', 'height', {}, NaN])
})
})
2 changes: 1 addition & 1 deletion src/libs/isTruthy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { Perhaps } from '../../types'
* Usage:
* arr.filter(isTruthy)
*/
export function isTruthy<T>(o: Perhaps<T | boolean | number | string>): o is T {
export function isTruthy<T>(o: Perhaps<T>): o is T {
return o !== undefined && o !== null && o !== false && o !== '' && o !== 0
}
19 changes: 17 additions & 2 deletions src/libs/isTruthy/isTruthy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@ import { isTruthy } from '.'

describe('isTruthy', () => {
it('should keep truthty values', () => {
const arr = [0, 1, 'two', undefined, 'four', 5, null, '', 'height', NaN]
const arr = [
0,
1,
'two',
undefined,
'four',
5,
null,
'',
'height',
{},
false,
NaN
]

expect(arr.filter(isTruthy)).toEqual([1, 'two', 'four', 5, 'height', NaN])
const results = arr.filter(isTruthy)

expect(results).toEqual([1, 'two', 'four', 5, 'height', {}, NaN])
})
})

0 comments on commit fc46b6d

Please sign in to comment.