Skip to content

Commit 22fff84

Browse files
committed
(refactor): whitelist and blacklist can be transforms
- so these can work as examples and so that their order is very explicit as they're treated like any other transform internally - recursive import of ITransform is a bit weird, but I figure it's better to have that visible in index.ts as opposed to in utils.ts or something - *shrug*, not horrible the other way I guess, just have to re-export
1 parent d06a164 commit 22fff84

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

src/blacklistTransform.ts

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

src/index.ts

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

33
import AsyncLocalStorage from './asyncLocalStorage'
4+
import { blacklistKeys } from './blacklistTransform'
5+
import { whitelistKeys } from './whitelistTransform'
6+
import { StrToAnyMap } from './utils'
47

58
export interface IArgs {
69
(name: string, store: IStateTreeNode, options?: IOptions): Promise<void>
@@ -12,7 +15,6 @@ export interface IOptions {
1215
readonly blacklist?: Array<string>,
1316
readonly transforms?: Array<ITransform>
1417
}
15-
type StrToAnyMap = {[key: string]: any}
1618

1719
export interface ITransform {
1820
readonly toStorage?: ITransformArgs,
@@ -40,19 +42,15 @@ export const persist: IArgs = (name, store, options = {}) => {
4042
}
4143

4244
if (!jsonify) { jsonify = true } // default to true like mobx-persist
43-
const whitelistDict = arrToDict(whitelist)
44-
const blacklistDict = arrToDict(blacklist)
45+
46+
transforms = [
47+
...(whitelist ? [whitelistKeys(whitelist)] : []),
48+
...(blacklist ? [blacklistKeys(blacklist)] : []),
49+
...(transforms || [])
50+
]
4551

4652
onSnapshot(store, (_snapshot: StrToAnyMap) => {
4753
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-
})
5654

5755
transforms.forEach((transform) => {
5856
if (transform.toStorage) { transform.toStorage(snapshot) }
@@ -77,16 +75,6 @@ export const persist: IArgs = (name, store, options = {}) => {
7775
})
7876
}
7977

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-
9078
function isString (value: any): value is string {
9179
return typeof value === 'string'
9280
}

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type StrToAnyMap = {[key: string]: any}
2+
3+
export type StrToBoolMap = {[key: string]: boolean}
4+
5+
export function arrToDict (arr?: Array<string>): StrToBoolMap {
6+
if (!arr) { return {} }
7+
return arr.reduce((dict: StrToBoolMap, elem) => {
8+
dict[elem] = true
9+
return dict
10+
}, {})
11+
}

src/whitelistTransform.ts

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

0 commit comments

Comments
 (0)