Skip to content

Commit

Permalink
(refactor): w/blist transform args shouldn't be optional
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
agilgur5 committed Nov 19, 2019
1 parent 00c1632 commit ae05316
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ interface ITransformArgs {
}
```

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.
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.

#### Transform Ordering

Expand Down
4 changes: 1 addition & 3 deletions src/blacklistTransform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { arrToDict } from './utils'
import { ITransform } from './index'

export function blacklistKeys (blacklist?: Array<string>): ITransform {
export function blacklistKeys (blacklist: Array<string>): ITransform {
const blacklistDict = arrToDict(blacklist)

return {toStorage: function blacklistTransform (snapshot) {
if (!blacklist) { return snapshot }

Object.keys(snapshot).forEach((key) => {
if (blacklistDict[key]) { delete snapshot[key] }
})
Expand Down
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ export type StrToAnyMap = {[key: string]: any}

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

export function arrToDict (arr?: Array<string>): StrToBoolMap {
if (!arr) { return {} }
export function arrToDict (arr: Array<string>): StrToBoolMap {
return arr.reduce((dict: StrToBoolMap, elem) => {
dict[elem] = true
return dict
Expand Down
4 changes: 1 addition & 3 deletions src/whitelistTransform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { arrToDict } from './utils'
import { ITransform } from './index'

export function whitelistKeys (whitelist?: Array<string>): ITransform {
export function whitelistKeys (whitelist: Array<string>): ITransform {
const whitelistDict = arrToDict(whitelist)

return {toStorage: function whitelistTransform (snapshot) {
if (!whitelist) { return snapshot }

Object.keys(snapshot).forEach((key) => {
if (!whitelistDict[key]) { delete snapshot[key] }
})
Expand Down

0 comments on commit ae05316

Please sign in to comment.