Skip to content

Commit bb7ec9a

Browse files
authored
Add transform option to flatParse
* Add `transform` option to `flatParse` * Filter null/undefined from transform * Use `Truthy<T>` to handle falsy values in `flatParse`
1 parent ab22ba4 commit bb7ec9a

6 files changed

Lines changed: 78 additions & 14 deletions

File tree

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default [
2828
name: 'merge'
2929
},
3030
{
31-
limit: '2.005 kB',
31+
limit: '2.04 kB',
3232
name: 'all',
3333
path: 'index.js'
3434
}

lib/parse/flat-parse.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ const itemEnhancer = opts => {
1313
}
1414
}
1515

16-
const bookmarksBuilder = ({ add, describe, enhance }) => {
16+
const bookmarksBuilder = ({ add, describe, enhance, transform }) => {
1717
const foldersStack = []
1818
let currFolder
1919

2020
return {
2121
addBookmark: (title, attrs) => {
2222
const bookmark = enhance({ folder: currFolder, title }, attrs)
23-
add(bookmark)
23+
const transformed = transform(bookmark)
24+
transformed && add(transformed)
2425
},
2526
closeFolder: () => {
2627
foldersStack.pop()
@@ -40,7 +41,8 @@ export const flatParse = (txt, opts = {}) => {
4041
const add = b => bookmarks.push(b)
4142
const describe = d => (bookmarks.at(-1).description = d)
4243
const enhance = itemEnhancer(opts)
43-
const handlers = bookmarksBuilder({ add, describe, enhance })
44+
const transform = opts.transform || (x => x)
45+
const handlers = bookmarksBuilder({ add, describe, enhance, transform })
4446
traverse(txt, handlers)
4547
return bookmarks
4648
}

readme.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ const bookmarks = flatParse(html)
135135

136136
You can configure the output by activating options passed as the second argument.
137137

138-
| Option | Type | Description |
139-
| -------------- | ---------- | ------------------------------------------------------------------------------------------- |
140-
| `excludeAttrs` | `string[]` | Excludes specified attributes from output. See [attributes definition](./types/attrs.d.ts). |
141-
| `withId` | `boolean` | Adds incremental numeric `id` to items. |
138+
| Option | Type | Description |
139+
| -------------- | --------------------------- | ------------------------------------------ |
140+
| `excludeAttrs` | `string[]` | Excludes specified attributes from output. |
141+
| `withId` | `boolean` | Adds incremental numeric `id` to items. |
142+
| `transform` | `(item: FlatBookmark) => T` | Transforms items, omitting falsy returns. |
142143

143144
### `customParse`
144145

@@ -183,7 +184,7 @@ const stringified = stringify(parsed)
183184

184185
Converts the flat list from `flatParse` back into an HTML string.
185186

186-
> It requires using `flatParse` with `{ withId: true }` to ensure unique item IDs.
187+
> It requires using `flatParse` with `{ withId: true }` to ensure unique item IDs, and without `transform` to ensure a known structure.
187188
188189
```js
189190
import { flatParse, flatStringify } from 'nbff-parser'

test/parse/flat-parse.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,41 @@ describe('flat-parse', () => {
185185
deepEqual(actual, expected)
186186
})
187187

188+
test('with transform', () => {
189+
const initial = `
190+
<DT><H3>JavaScript</H3>
191+
<DL><p>
192+
<DT><A HREF="https://tc39.es/" PERSONAL_TOOLBAR_FOLDER="true">TC39 - Specifying JavaScript.</A>
193+
<DT><H3>Engines</H3>
194+
<DL><p>
195+
<DT><A HREF="https://v8.dev/">V8 JavaScript engine</A>
196+
</DL><p>
197+
</DL><p>
198+
`
199+
200+
const transform = item => {
201+
if (item.personal_toolbar_folder) return
202+
203+
return {
204+
tag: item.folder.map(f => f.title).join(' / '),
205+
title: item.title,
206+
url: item.href
207+
}
208+
}
209+
210+
const actual = flatParse(initial, { transform })
211+
212+
const expected = [
213+
{
214+
tag: 'JavaScript / Engines',
215+
title: 'V8 JavaScript engine',
216+
url: 'https://v8.dev/'
217+
}
218+
]
219+
220+
deepEqual(actual, expected)
221+
})
222+
188223
describe('bookmarks-1.html', () => {
189224
const initial = readFile('./bookmarks-1.html')
190225

test/parse/flat-parse.types.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FlatBookmark, FlatBookmarkWithId } from '../../index.js'
1+
import { FlatBookmark, FlatBookmarkWithId, flatParse } from '../../index.js'
22

33
// We check the relevance of types here via `pnpm test:types`.
44

@@ -45,3 +45,25 @@ const bookmarkWithId: Required<FlatBookmarkWithId> = {
4545
],
4646
id: 0
4747
}
48+
49+
const parser = flatParse('..')
50+
parser satisfies FlatBookmark[]
51+
52+
const parserWithId = flatParse('..', { withId: true })
53+
parserWithId satisfies FlatBookmark[]
54+
parserWithId satisfies FlatBookmarkWithId[]
55+
56+
const parserWithTransform = flatParse('..', {
57+
transform: item => {
58+
if (!item.href) return
59+
return ({ name: item.title, url: item.href })
60+
}
61+
})
62+
parserWithTransform satisfies { name: string, url: string }[]
63+
64+
const parserWithIdent = flatParse('..', { transform: item => item })
65+
parserWithIdent satisfies FlatBookmark[]
66+
67+
const parserWithIdAndIdent = flatParse('..', { withId: true, transform: item => item })
68+
parserWithIdAndIdent satisfies FlatBookmark[]
69+
parserWithIdAndIdent satisfies FlatBookmarkWithId[]

types/parse/flat-parse.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ export type FlatBookmarkWithId = WithId<
1515
}
1616
>
1717

18+
type Truthy<V> = V extends null | undefined | false | 0 | "" ? never : V
19+
1820
// Overload signatures.
19-
export function flatParse(
21+
export function flatParse<T = FlatBookmark>(
2022
text: string,
2123
options?: Partial<{
2224
excludeAttrs: AllAttrKeys[]
2325
withId: false
26+
transform: (item: FlatBookmark) => T
2427
}>
25-
): FlatBookmark[]
26-
export function flatParse(
28+
): Truthy<T>[]
29+
export function flatParse<T = FlatBookmarkWithId>(
2730
text: string,
2831
options: {
2932
excludeAttrs?: AllAttrKeys[]
3033
withId: true
34+
transform?: (item: FlatBookmarkWithId) => T
3135
}
32-
): FlatBookmarkWithId[]
36+
): Truthy<T>[]

0 commit comments

Comments
 (0)