Skip to content

Commit

Permalink
feat: add a constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Jan 6, 2025
1 parent b32d121 commit 8bb8a6f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/modules/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Added in v0.1.0
<h2 class="text-delta">Table of contents</h2>

- [constructors](#constructors)
- [Uuid](#uuid)
- [v4](#v4)
- [model](#model)
- [Uuid (type alias)](#uuid-type-alias)
Expand All @@ -23,6 +24,26 @@ Added in v0.1.0

# constructors

## Uuid

**Signature**

```ts
export declare function Uuid(uuid: string): Uuid
```

**Example**

```ts
import { Uuid } from 'uuid-ts'
const uuid = Uuid('224d8877-d59f-409f-aed0-5157df78357f')
assert.deepStrictEqual(uuid, '224d8877-d59f-409f-aed0-5157df78357f')
```

Added in v0.1.1

## v4

**Signature**
Expand Down
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ interface UuidBrand<V extends UuidVersion> {
// constructors
// -------------------------------------------------------------------------------------

/**
* @example
* import { Uuid } from 'uuid-ts'
*
* const uuid = Uuid('224d8877-d59f-409f-aed0-5157df78357f')
*
* assert.deepStrictEqual(uuid, '224d8877-d59f-409f-aed0-5157df78357f')
*
* @category constructors
* @since 0.1.1
*/
export function Uuid(uuid: string): Uuid {
if (!isUuid(uuid)) {
throw new Error('Not a UUID')
}

return uuid
}

/**
* @category constructors
* @since 0.1.0
Expand Down
7 changes: 7 additions & 0 deletions test-d/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ expectTypeOf(uuid1).toMatchTypeOf(uuid)
expectTypeOf(uuid).not.toMatchTypeOf(uuid1)
expectTypeOf(uuid1).not.toMatchTypeOf(uuid2)

//
// Uuid
//

expectTypeOf(_.Uuid(string)).toMatchTypeOf<Uuid>()
expectTypeOf(_.Uuid(uuid)).toMatchTypeOf<Uuid>()

//
// v4
//
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import * as _ from '../src'

describe('uuid-ts', () => {
describe('constructors', () => {
describe('Uuid', () => {
test('with an UUID', () => {
fc.assert(
fc.property(fc.uuid(), uuid => {
expect(_.Uuid(uuid)).toStrictEqual(uuid)
}),
)
})

test('with a non-UUID', () => {
fc.assert(
fc.property(fc.anything(), value => {
expect(() => _.Uuid(value as string)).toThrow(new Error('Not a UUID'))
}),
)
})
})

describe('v4', () => {
test('generates a v4 UUID', () => {
const uuid = _.v4()()
Expand Down

0 comments on commit 8bb8a6f

Please sign in to comment.