Skip to content

Commit a4d8f16

Browse files
committed
(refactor): whitelist and blacklist as transforms
- so that these can work as examples and so that their order is very explicit as they're treated like any other transform internally - create new transforms/ directory that will hold all built-in transforms moving forward - and export them in transforms/index.ts
1 parent 8bba87c commit a4d8f16

File tree

6 files changed

+61
-29
lines changed

6 files changed

+61
-29
lines changed

src/index.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { onSnapshot, applySnapshot, IStateTreeNode } from 'mobx-state-tree'
22

33
import AsyncLocalStorage from './asyncLocalStorage'
4+
import { ITransform, whitelistKeys, blacklistKeys } from './transforms/index'
5+
import { StrToAnyMap } from './utils'
46

57
export interface IArgs {
68
(name: string, store: IStateTreeNode, options?: IOptions): Promise<void>
@@ -12,15 +14,7 @@ export interface IOptions {
1214
readonly blacklist?: Array<string>,
1315
readonly transforms?: Array<ITransform>
1416
}
15-
type StrToAnyMap = {[key: string]: any}
16-
17-
export interface ITransform {
18-
readonly toStorage?: ITransformArgs,
19-
readonly fromStorage?: ITransformArgs
20-
}
21-
export interface ITransformArgs {
22-
(snapshot: StrToAnyMap): StrToAnyMap
23-
}
17+
export { ITransform, ITransformArgs } from './transforms/index'
2418

2519
export const persist: IArgs = (name, store, options = {}) => {
2620
let {storage, jsonify = true, whitelist, blacklist, transforms = []} = options
@@ -39,20 +33,16 @@ export const persist: IArgs = (name, store, options = {}) => {
3933
'engine via the `storage:` option.')
4034
}
4135

42-
const whitelistDict = arrToDict(whitelist)
43-
const blacklistDict = arrToDict(blacklist)
36+
// whitelist, blacklist, then any custom transforms
37+
transforms = [
38+
...(whitelist ? [whitelistKeys(whitelist)] : []),
39+
...(blacklist ? [blacklistKeys(blacklist)] : []),
40+
...transforms
41+
]
4442

4543
onSnapshot(store, (_snapshot: StrToAnyMap) => {
4644
// need to shallow clone as otherwise properties are non-configurable (https://github.com/agilgur5/mst-persist/pull/21#discussion_r348105595)
4745
const snapshot = { ..._snapshot }
48-
Object.keys(snapshot).forEach((key) => {
49-
if (whitelist && !whitelistDict[key]) {
50-
delete snapshot[key]
51-
}
52-
if (blacklist && blacklistDict[key]) {
53-
delete snapshot[key]
54-
}
55-
})
5646

5747
transforms.forEach((transform) => {
5848
if (transform.toStorage) { transform.toStorage(snapshot) }
@@ -77,16 +67,6 @@ export const persist: IArgs = (name, store, options = {}) => {
7767
})
7868
}
7969

80-
type StrToBoolMap = {[key: string]: boolean}
81-
82-
function arrToDict (arr?: Array<string>): StrToBoolMap {
83-
if (!arr) { return {} }
84-
return arr.reduce((dict: StrToBoolMap, elem) => {
85-
dict[elem] = true
86-
return dict
87-
}, {})
88-
}
89-
9070
function isString (value: any): value is string {
9171
return typeof value === 'string'
9272
}

src/transforms/blacklist.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ITransform, arrToDict } from './utils'
2+
3+
export function blacklistKeys (blacklist?: Array<string>): ITransform {
4+
const blacklistDict = arrToDict(blacklist)
5+
6+
return {toStorage: function blacklistTransform (snapshot) {
7+
Object.keys(snapshot).forEach((key) => {
8+
if (blacklist && blacklistDict[key]) {
9+
delete snapshot[key]
10+
}
11+
})
12+
return snapshot
13+
}}
14+
}

src/transforms/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { ITransform, ITransformArgs } from './utils'
2+
3+
export { whitelistKeys } from './whitelist'
4+
export { blacklistKeys } from './blacklist'

src/transforms/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { StrToAnyMap } from '../utils'
2+
3+
export interface ITransform {
4+
readonly toStorage?: ITransformArgs,
5+
readonly fromStorage?: ITransformArgs
6+
}
7+
export interface ITransformArgs {
8+
(snapshot: StrToAnyMap): StrToAnyMap
9+
}
10+
11+
type StrToBoolMap = {[key: string]: boolean}
12+
13+
export function arrToDict (arr?: Array<string>): StrToBoolMap {
14+
if (!arr) { return {} }
15+
return arr.reduce((dict: StrToBoolMap, elem) => {
16+
dict[elem] = true
17+
return dict
18+
}, {})
19+
}

src/transforms/whitelist.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ITransform, arrToDict } from './utils'
2+
3+
export function whitelistKeys (whitelist?: Array<string>): ITransform {
4+
const whitelistDict = arrToDict(whitelist)
5+
6+
return {toStorage: function whitelistTransform (snapshot) {
7+
Object.keys(snapshot).forEach((key) => {
8+
if (whitelist && !whitelistDict[key]) {
9+
delete snapshot[key]
10+
}
11+
})
12+
return snapshot
13+
}}
14+
}

src/utils.ts

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

0 commit comments

Comments
 (0)