|
1 | | -# pii |
2 | | - |
3 | | -[](https://codeclimate.com/github/tdreyno/pii/test_coverage) |
4 | | -[](https://www.npmjs.com/package/@tdreyno/pii) |
5 | | -[](https://bundlephobia.com/result?p=@tdreyno/pii) |
6 | | - |
7 | | -pii is library for wrapping Personally Identifying Information and providing ways to limit its spread. |
8 | | - |
9 | | -## Installation |
10 | | - |
11 | | -### Yarn |
12 | | - |
13 | | -```sh |
14 | | -yarn add @tdreyno/pii |
15 | | -``` |
16 | | - |
17 | | -### NPM |
18 | | - |
19 | | -```sh |
20 | | -npm install --save @tdreyno/pii |
21 | | -``` |
22 | | - |
23 | | -## Usage |
24 | | - |
25 | | -The concept is simple. Wrap `PII` around any PII data **as soon as it enters your system.** |
26 | | - |
27 | | -```typescript |
28 | | -import { PII, unwrapObject } from "@tdreyno/pii" |
29 | | - |
30 | | -const handlePost = (postBody: { name: string; phoneNumber: string }) => |
31 | | - createUser(PII(postBody.name), PII(postBody.phoneNumber)) |
32 | | - |
33 | | -const createUser = (name: PII<string>, phoneNumber: PII<string>) => |
34 | | - sendToThirdParty(unwrapObject({ name, phoneNumber })) |
35 | | -``` |
36 | | - |
37 | | -`PII` wraps any data and obscures the details. Attempts to coerce it to other types or convert to JSON will result in a string result of `PII<REDACTED>`. Once the PII has flowed through your system, use `unwrap` to get the data back out for posting to a third party. Or `unwrapObject` which will recursively unwrap PII inside a post body object. |
38 | | - |
39 | | -### Avoiding unsafe unwrapping and rewrapping |
40 | | - |
41 | | -If you want to combine two pieces of PII, such as first and last name, you might want to unwrap, combine and then rewrap them. Once you start using unwrap everywhere, it becomes very hard to maintain safetly. Rather, use the built in methods of this library to mutate and combine PII while keeping the data inside the wrapper. |
42 | | - |
43 | | -#### Modify PII |
44 | | - |
45 | | -```typescript |
46 | | -import { PII, map } from "@tdreyno/pii" |
47 | | - |
48 | | -const name = PII("Thomas") |
49 | | -const lowercaseName = map(n => n.toLowerCase(), name) // PII<"thomas"> |
50 | | -``` |
51 | | - |
52 | | -#### Combine two things |
53 | | - |
54 | | -```typescript |
55 | | -import { PII, zip2With } from "@tdreyno/pii" |
56 | | - |
57 | | -const firstName = PII("Thomas") |
58 | | -const lastName = PII("Reynolds") |
59 | | -const fullName = zip2With( |
60 | | - (first, last) => `${first} ${last}`, |
61 | | - firstName, |
62 | | - lastName, |
63 | | -) // PII<Thomas Reynolds> |
64 | | -``` |
65 | | - |
66 | | -**Note:** There are methods for up to 4 PII inputs: `zip2With`, `zip3With`, `zip4With`. |
67 | | - |
68 | | -#### Reduce a list of PII |
69 | | - |
70 | | -```typescript |
71 | | -import { PII, fold } from "@tdreyno/pii" |
72 | | - |
73 | | -const address1 = PII(ADDRESS_OBJECT_1) |
74 | | -const address2 = PII(ADDRESS_OBJECT_2) |
75 | | -const address2 = PII(ADDRESS_OBJECT_2) |
76 | | - |
77 | | -const allAddresses = fold( |
78 | | - (acc, address) => (acc.push(address), acc), |
79 | | - [], |
80 | | - [address1, address2, address3], |
81 | | -) // PII<[address1, address2, address3]> |
82 | | -``` |
83 | | - |
84 | | -#### Inspect or create side-effects using PII contents without unwrapping |
85 | | - |
86 | | -```typescript |
87 | | -import { PII, tap } from "@tdreyno/pii" |
88 | | - |
89 | | -const name = PII("Thomas") |
90 | | -const lowercaseName = tap(n => console.log(n), name) // Logs "Thomas" |
91 | | -``` |
92 | | - |
93 | | -## License |
94 | | - |
95 | | -pii is licensed under the Hippocratic License. It is an [Ethical Source license](https://ethicalsource.dev) derived from the MIT License, amended to limit the impact of the unethical use of open source software. |
| 1 | +# pii |
| 2 | + |
| 3 | +[](https://codeclimate.com/github/tdreyno/pii/test_coverage) |
| 4 | +[](https://www.npmjs.com/package/@tdreyno/pii) |
| 5 | +[](https://bundlephobia.com/result?p=@tdreyno/pii) |
| 6 | + |
| 7 | +pii is library for wrapping Personally Identifying Information and providing ways to limit its spread. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +### Yarn |
| 12 | + |
| 13 | +```sh |
| 14 | +yarn add @tdreyno/pii |
| 15 | +``` |
| 16 | + |
| 17 | +### NPM |
| 18 | + |
| 19 | +```sh |
| 20 | +npm install --save @tdreyno/pii |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +The concept is simple. Wrap `PII` around any PII data **as soon as it enters your system.** |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { PII, unwrapObject } from "@tdreyno/pii" |
| 29 | + |
| 30 | +const handlePost = (postBody: { name: string; phoneNumber: string }) => |
| 31 | + createUser(PII(postBody.name), PII(postBody.phoneNumber)) |
| 32 | + |
| 33 | +const createUser = (name: PII<string>, phoneNumber: PII<string>) => |
| 34 | + sendToThirdParty(unwrapObject({ name, phoneNumber })) |
| 35 | +``` |
| 36 | + |
| 37 | +`PII` wraps any data and obscures the details. Attempts to coerce it to other types or convert to JSON will result in a string result of `PII<REDACTED>`. Once the PII has flowed through your system, use `unwrap` to get the data back out for posting to a third party. Or `unwrapObject` which will recursively unwrap PII inside a post body object. |
| 38 | + |
| 39 | +### Avoiding unsafe unwrapping and rewrapping |
| 40 | + |
| 41 | +If you want to combine two pieces of PII, such as first and last name, you might want to unwrap, combine and then rewrap them. Once you start using unwrap everywhere, it becomes very hard to maintain safetly. Rather, use the built in methods of this library to mutate and combine PII while keeping the data inside the wrapper. |
| 42 | + |
| 43 | +#### Modify PII |
| 44 | + |
| 45 | +```typescript |
| 46 | +import { PII, map } from "@tdreyno/pii" |
| 47 | + |
| 48 | +const name = PII("Thomas") |
| 49 | +const lowercaseName = map(n => n.toLowerCase(), name) // PII<"thomas"> |
| 50 | +``` |
| 51 | + |
| 52 | +#### Combine two things |
| 53 | + |
| 54 | +```typescript |
| 55 | +import { PII, zip2With } from "@tdreyno/pii" |
| 56 | + |
| 57 | +const firstName = PII("Thomas") |
| 58 | +const lastName = PII("Reynolds") |
| 59 | +const fullName = zip2With( |
| 60 | + (first, last) => `${first} ${last}`, |
| 61 | + firstName, |
| 62 | + lastName, |
| 63 | +) // PII<Thomas Reynolds> |
| 64 | +``` |
| 65 | + |
| 66 | +**Note:** There are methods for up to 4 PII inputs: `zip2With`, `zip3With`, `zip4With`. |
| 67 | + |
| 68 | +#### Reduce a list of PII |
| 69 | + |
| 70 | +```typescript |
| 71 | +import { PII, fold } from "@tdreyno/pii" |
| 72 | + |
| 73 | +const address1 = PII(ADDRESS_OBJECT_1) |
| 74 | +const address2 = PII(ADDRESS_OBJECT_2) |
| 75 | +const address2 = PII(ADDRESS_OBJECT_2) |
| 76 | + |
| 77 | +const allAddresses = fold( |
| 78 | + (acc, address) => (acc.push(address), acc), |
| 79 | + [], |
| 80 | + [address1, address2, address3], |
| 81 | +) // PII<[address1, address2, address3]> |
| 82 | +``` |
| 83 | + |
| 84 | +#### Inspect or create side-effects using PII contents without unwrapping |
| 85 | + |
| 86 | +```typescript |
| 87 | +import { PII, tap } from "@tdreyno/pii" |
| 88 | + |
| 89 | +const name = PII("Thomas") |
| 90 | +const lowercaseName = tap(n => console.log(n), name) // Logs "Thomas" |
| 91 | +``` |
| 92 | + |
| 93 | +#### Custom PII Redaction |
| 94 | + |
| 95 | +```typescript |
| 96 | +import { PII, redact } from "@tdreyno/pii" |
| 97 | + |
| 98 | +const name = PII("Thomas") |
| 99 | +const lowercaseName = redact(() => "REDACTED", name) // Returns "REDACTED" |
| 100 | +``` |
| 101 | + |
| 102 | +## License |
| 103 | + |
| 104 | +pii is licensed under the Hippocratic License. It is an [Ethical Source license](https://ethicalsource.dev) derived from the MIT License, amended to limit the impact of the unethical use of open source software. |
0 commit comments