Skip to content

Commit 5ddd43e

Browse files
committed
(refactor): w/blist transform args shouldn't be optional
- the array passed into the transforms should be required - same for arrToDict function - they're now called only when array is defined, and I think that behavior is more intuitive -- it should error if the array isn't defined - (test): in internal usage, they're never undefined, so this was reducing coverage because it was dead code - if the transforms were to eventually be externally visible / import-able, they should definitely throw an error in that case too -- if external users pass undefined to it - and the typings change means it'll happen at design-time too - definitely think it's more intuitive for external users this way too - (docs): use this commit for w/blist transform examples - or a variant of this commit -- just one where arr is required
1 parent e688152 commit 5ddd43e

File tree

4 files changed

+4
-9
lines changed

4 files changed

+4
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ interface ITransformArgs {
8484
type StrToAnyMap = {[key: string]: any}
8585
```
8686
87-
As an example, one may see how [whitelists](https://github.com/agilgur5/mst-persist/blob/229fd2b1b472ea6a7912a5a06fa079a65e3ba6fa/src/whitelistTransform.ts#L7-L14) and [blacklists](https://github.com/agilgur5/mst-persist/blob/229fd2b1b472ea6a7912a5a06fa079a65e3ba6fa/src/blacklistTransform.ts#L7-L14) are implemented internally as transforms.
87+
As an example, one may see how [whitelists](https://github.com/agilgur5/mst-persist/blob/9ba76aaf455f42e249dc855d66349351148a17da/src/whitelistTransform.ts#L7-L12) and [blacklists](https://github.com/agilgur5/mst-persist/blob/9ba76aaf455f42e249dc855d66349351148a17da/src/blacklistTransform.ts#L7-L12) are implemented internally as transforms.
8888
8989
#### Transform Ordering
9090

src/blacklistTransform.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { arrToDict } from './utils'
22
import { ITransform } from './index'
33

4-
export function blacklistKeys (blacklist?: Array<string>): ITransform {
4+
export function blacklistKeys (blacklist: Array<string>): ITransform {
55
const blacklistDict = arrToDict(blacklist)
66

77
return {toStorage: function blacklistTransform (snapshot) {
8-
if (!blacklist) { return snapshot }
9-
108
Object.keys(snapshot).forEach((key) => {
119
if (blacklistDict[key]) { delete snapshot[key] }
1210
})

src/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ export type StrToAnyMap = {[key: string]: any}
22

33
export type StrToBoolMap = {[key: string]: boolean}
44

5-
export function arrToDict (arr?: Array<string>): StrToBoolMap {
6-
if (!arr) { return {} }
5+
export function arrToDict (arr: Array<string>): StrToBoolMap {
76
return arr.reduce((dict: StrToBoolMap, elem) => {
87
dict[elem] = true
98
return dict

src/whitelistTransform.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { arrToDict } from './utils'
22
import { ITransform } from './index'
33

4-
export function whitelistKeys (whitelist?: Array<string>): ITransform {
4+
export function whitelistKeys (whitelist: Array<string>): ITransform {
55
const whitelistDict = arrToDict(whitelist)
66

77
return {toStorage: function whitelistTransform (snapshot) {
8-
if (!whitelist) { return snapshot }
9-
108
Object.keys(snapshot).forEach((key) => {
119
if (!whitelistDict[key]) { delete snapshot[key] }
1210
})

0 commit comments

Comments
 (0)