Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
refactor: new API
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Dec 11, 2023
1 parent 88a1448 commit b50a787
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
## Quickstart

```js
import { snippetz, undici } from '@scalar/snippetz'

const snippet = snippetz().get(
undici({
url: 'https://example.com'
})
)
```

<!-- ```js
import { format, print, undici } from '@scalar/snippetz'
const tree = undici({
url: 'https://example.com'
})
console.log(format(print(tree)))
```
``` -->

Output:

Expand Down
3 changes: 2 additions & 1 deletion packages/snippetz/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './undici'
export * from './format'
export * from './print'
export * from './snippetz'
export * from './undici'
31 changes: 31 additions & 0 deletions packages/snippetz/src/snippetz.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, test } from 'vitest'
import { snippetz } from './snippetz'

const tree = {
type: 'Program',
body: [
{
type: 'VariableDeclaration',
declarations: [
{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: 'answer',
},
init: {
type: 'Literal',
value: 42,
},
},
],
kind: 'const',
},
],
}

test('snippetz', async () => {
const snippet = await snippetz().get(tree)

expect(snippet).toBe(`const answer = 42\n`)
})
20 changes: 20 additions & 0 deletions packages/snippetz/src/snippetz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { format } from './format'
import { print } from './print'

export type SnippetOptions = {}

export class Snippetz {
constructor(options?: SnippetOptions) {
// console.log(options)
}

get(tree: any) {
return format(print(tree))
}
}

export function snippetz(tree: any) {
return new Snippetz({
tree,
})
}

0 comments on commit b50a787

Please sign in to comment.