Skip to content

Commit

Permalink
rename filter and sort methods
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Nov 24, 2024
1 parent 8476844 commit 155f2e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
10 changes: 10 additions & 0 deletions .changeset/sour-lamps-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'renoun': minor
---

Renames file system methods `filter` to `withFilter` and `sort` to `withSort` for better clarity since they are not immediately applied.

### Breaking Changes

- `<Directory>.filter` method is now `<Directory>.withFilter`
- `<Directory>.sort` method is now `<Directory>.withSort`
14 changes: 7 additions & 7 deletions packages/renoun/src/file-system/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ describe('file system', () => {
path: 'posts',
fileSystem,
})
.filter((entry) => isFile(entry, 'mdx'))
.sort((a, b) => a.getName().localeCompare(b.getName()))
.withFilter((entry) => isFile(entry, 'mdx'))
.withSort((a, b) => a.getName().localeCompare(b.getName()))
const files = await posts.getEntries()

expectTypeOf(files).toMatchTypeOf<JavaScriptFile<PostType>[]>()
Expand All @@ -144,7 +144,7 @@ describe('file system', () => {
type PostType = { frontmatter: { title: string } }
const posts = new Directory<{ mdx: PostType }>({
path: 'fixtures/posts',
}).filter((entry) => isFile(entry, 'mdx'))
}).withFilter((entry) => isFile(entry, 'mdx'))
const files = await posts.getEntries()

expectTypeOf(files).toMatchTypeOf<JavaScriptFile<PostType>[]>()
Expand All @@ -156,7 +156,7 @@ describe('file system', () => {
'foo.ts': '',
'bar.ts': '',
})
const directory = new Directory({ fileSystem }).sort((a, b) =>
const directory = new Directory({ fileSystem }).withSort((a, b) =>
a.getName().localeCompare(b.getName())
)
const entries = await directory.getEntries()
Expand Down Expand Up @@ -185,8 +185,8 @@ describe('file system', () => {
}
},
})
.filter((entry) => isFile(entry, 'ts'))
.sort(async (a, b) => {
.withFilter((entry) => isFile(entry, 'ts'))
.withSort(async (a, b) => {
const aSort = await a.getExport('sort').getRuntimeValue()
const bSort = await b.getExport('sort').getRuntimeValue()
return aSort - bSort
Expand All @@ -202,7 +202,7 @@ describe('file system', () => {
})

test('filter and recursive entries', async () => {
const docs = new Directory({ path: 'fixtures/docs' }).filter((entry) =>
const docs = new Directory({ path: 'fixtures/docs' }).withFilter((entry) =>
isFile(entry, 'mdx')
)
const entries = await docs.getEntries({ recursive: true })
Expand Down
20 changes: 10 additions & 10 deletions packages/renoun/src/file-system/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -566,14 +566,14 @@ export class Directory<
}

/** Returns a new `Directory` with a narrowed type and filter applied to all descendant entries. */
filter<FilteredEntry extends Entry>(
filterFn: (entry: FileSystemEntry<Types>) => entry is FilteredEntry
withFilter<FilteredEntry extends Entry>(
filter: (entry: FileSystemEntry<Types>) => entry is FilteredEntry
): Directory<Types, FilteredEntry>
filter<FilteredEntry extends Entry>(
filterFn: (entry: FileSystemEntry<Types>) => Promise<boolean> | boolean
withFilter<FilteredEntry extends Entry>(
filter: (entry: FileSystemEntry<Types>) => Promise<boolean> | boolean
): Directory<Types, Entry>
filter<FilteredEntry extends Entry>(
filterFn: (entry: FileSystemEntry<Types>) => Promise<boolean> | boolean
withFilter<FilteredEntry extends Entry>(
filter: (entry: FileSystemEntry<Types>) => Promise<boolean> | boolean
): Directory<Types, Entry | FilteredEntry> {
const filteredDirectory = new Directory<Types, Entry | FilteredEntry>({
path: this.#path,
Expand All @@ -584,7 +584,7 @@ export class Directory<
})

filteredDirectory.setDirectory(this)
filteredDirectory.setFilterCallback(filterFn)
filteredDirectory.setFilterCallback(filter)
if (this.#sortCallback) {
filteredDirectory.setSortCallback(this.#sortCallback)
}
Expand All @@ -603,8 +603,8 @@ export class Directory<
}

/** Returns a new `Directory` with a sorting function applied to all descendant entries. */
sort(
sortCallback: (a: Entry, b: Entry) => Promise<number> | number
withSort(
sort: (a: Entry, b: Entry) => Promise<number> | number
): Directory<Types, Entry> {
const sortedDirectory = new Directory<Types, Entry>({
path: this.#path,
Expand All @@ -615,7 +615,7 @@ export class Directory<
})

sortedDirectory.setDirectory(this)
sortedDirectory.setSortCallback(sortCallback)
sortedDirectory.setSortCallback(sort)
if (this.#filterCallback) {
sortedDirectory.setFilterCallback(this.#filterCallback)
}
Expand Down

0 comments on commit 155f2e7

Please sign in to comment.