Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option #6

Merged
merged 40 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b8afbd9
docs(option): Move Option module documentation to mod.ts
suddenlyGiovanni May 28, 2024
4aa1659
docs(option): Update `equal` examples documentation
suddenlyGiovanni May 28, 2024
07e7c00
feat(option): Add 'get' method to Option class and corresponding tests
suddenlyGiovanni May 28, 2024
56ca5fc
feat(option): Add `isEmpty` functionality to Option class
suddenlyGiovanni May 28, 2024
be58850
docs: Update styling for examples in option.ts documentation
suddenlyGiovanni May 28, 2024
6d1d3cc
test: Add `@std/expect` dependency and use in tests
suddenlyGiovanni May 28, 2024
c0137a9
type(internal): Add `Lazy` interface in function.ts
suddenlyGiovanni May 28, 2024
9465985
fix(option): Refine Option class and add isEmpty method
suddenlyGiovanni May 28, 2024
2dfbf56
revert(option): Add fold method to Option class
suddenlyGiovanni May 28, 2024
c53c11a
revert(option): remove Some value getter
suddenlyGiovanni May 28, 2024
1dae518
chore: Added lock property to deno.jsonc and updated deno.xml options
suddenlyGiovanni May 28, 2024
6cada52
test(option): refactor tests for clarity
suddenlyGiovanni May 28, 2024
ffea28e
types(internal): Add 'types.ts' for utilities handling TypeScript types
suddenlyGiovanni May 28, 2024
ba4710f
refactor: imports and update dependencies
suddenlyGiovanni May 30, 2024
6b88796
test(option): Update Option.fromNullable tests and documentation
suddenlyGiovanni May 30, 2024
df91f5c
docs: Update import statements and type declarations in doc blocks
suddenlyGiovanni May 30, 2024
c24ab88
test(option): Update fold function signature and add tests
suddenlyGiovanni May 30, 2024
5b9b8a5
test(option): add static fold method
suddenlyGiovanni May 30, 2024
d42f42e
test(option): Added example usage to Option methods documentation
suddenlyGiovanni May 30, 2024
d83407d
refactor(option): made static `Option.fold` curried
suddenlyGiovanni May 30, 2024
b4d1ff9
feat(option): add static match method
suddenlyGiovanni May 31, 2024
e7f7418
chore: Update scripts for doc:lint and doc:test in deno.jsonc
suddenlyGiovanni May 31, 2024
5494d7e
types(option): Add Value type utility and corresponding tests
suddenlyGiovanni May 31, 2024
f03a2ca
fix(option): Refactor Option.None and fix related tests
suddenlyGiovanni May 31, 2024
256a2a0
refactor(option): Update fold method in Option class and tests
suddenlyGiovanni May 31, 2024
e4c2f84
fix
suddenlyGiovanni Jun 2, 2024
f5caa4f
test(option): Refactor `option.test.ts` to use `pipe` function
suddenlyGiovanni Jun 3, 2024
009bc3f
docs(option): Refactor pattern matching in Option class
suddenlyGiovanni Jun 3, 2024
1162129
style: Deno fmt
suddenlyGiovanni Jun 3, 2024
ba37e1c
docs(option): Refactor fold function in option.ts
suddenlyGiovanni Jun 3, 2024
3f3b43b
chore: Add @biomejs/biome package to dependencies
suddenlyGiovanni Jun 3, 2024
eb9866f
refactor(option): extend 'match' function return type for added flexi…
suddenlyGiovanni Jun 3, 2024
88832c0
test(internal): Add tests for utility types in types.ts
suddenlyGiovanni Jun 3, 2024
9edfc02
refactor(option): Change Option classes visibility to internal and ex…
suddenlyGiovanni Jun 3, 2024
86c12e5
feat(option): Add pattern matching utility to Option module
suddenlyGiovanni Jun 3, 2024
2a856e2
docs(option): Refactor option.ts for more informative error messages …
suddenlyGiovanni Jun 3, 2024
d5c89bd
docs(option): Add equivalent examples to function documentation
suddenlyGiovanni Jun 3, 2024
0c9b5c6
feat(option): Add `getOrElse` method and unit tests
suddenlyGiovanni Jun 3, 2024
dfe0d38
docs(option): Update Option documentation
suddenlyGiovanni Jun 3, 2024
8cafd60
docs(option): Enhance documentation in option.ts
suddenlyGiovanni Jun 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/deno.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@suddenly-giovanni/std",
"version": "0.0.4",
"lock": true,
"exports": {
"./predicate": "./src/predicate/mod.ts",
"./option": "./src/option/mod.ts"
Expand Down
9 changes: 9 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/internal/function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
// deno-lint-ignore-file ban-types

/**
* A lazy computation that can be used to defer the evaluation of an expression.
*
* @example
* To defer the evaluation of an expression:
* ```ts
* import { type Lazy } from './function.ts'
* declare function fib(n: number): number
*
* const lazy: Lazy<number> = () => fib(100) // fib is not called yet, hence the computation is deferred
* ```
*
* @example
* As a parameter type in a function:
* ```ts
* import { type Lazy } from './function.ts'
* function defer<A>(f: Lazy<A>): A {
* return f()
* }
* const value = defer(() => 1) // 1
* ```
*/
export interface Lazy<A> {
// biome-ignore lint/style/useShorthandFunctionType: we want the opaque type here
(): A
}

/**
* Pipes the value of an expression into a pipeline of functions.
*
Expand Down
23 changes: 23 additions & 0 deletions src/internal/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expectTypeOf } from 'npm:expect-type@0.19.0'
import { describe, test } from 'jsr:@std/testing/bdd'
import type { Types } from './types.ts'

describe('Types', () => {
test('Tag', () => {
expectTypeOf<Types.Tags<string | { _tag: 'a' } | { _tag: 'b' }> & unknown>().toEqualTypeOf<
'a' | 'b'
>()
})

test('Equal', () => {
expectTypeOf<Types.Equals<{ a: number }, { a: number }>>().toEqualTypeOf<true>()
expectTypeOf<Types.Equals<{ a: number }, { b: number }>>().toEqualTypeOf<false>()
})

test('Simplify', () => {
expectTypeOf<Types.Simplify<object & { a: number } & { b: number }>>().toEqualTypeOf<{
a: number
b: number
}>()
})
})
66 changes: 66 additions & 0 deletions src/internal/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* This namespace provides utility types for type manipulation.
*
* @example
* ```ts
* import type { Types } from './types.ts'
*
* type Test1 = Types.Tags<string | { _tag: 'a' } | { _tag: 'b'} >
* // ^? "a" | "b"
*
* type Test2 = Types.Equals<{ a: number }, { a: number }>
* // ^? true
*
* type Test3 = Types.Simplify<{ a: number } & { b: number }>
* // ^? { a: number; b: number; }
*
* ```
*/
export declare namespace Types {
/**
* Returns the tags in a type.
*
* @example
* ```ts
* import type { Types } from './types.ts'
*
* type Res = Types.Tags<string | { _tag: 'a' } | { _tag: 'b'} > // "a" | "b"
* ```
*
* @category types
*/
export type Tags<E> = E extends { _tag: string } ? E['_tag'] : never

/**
* Determines if two types are equal.
*
* @example
* ```ts
* import type { Types } from './types.ts'
*
* type Res1 = Types.Equals<{ a: number }, { a: number }> // true
* type Res2 = Types.Equals<{ a: number }, { b: number }> // false
* ```
*
* @category models
*/
export type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
? true
: false

/**
* Simplifies the type signature of a type.
*
* @example
* ```ts
* import type { Types } from './types.ts'
*
* type Res = Types.Simplify<{ a: number } & { b: number }> // { a: number; b: number; }
* ```
* @category types
*/
export type Simplify<A> = {
[K in keyof A]: A[K]
} extends infer B ? B
: never
}
43 changes: 43 additions & 0 deletions src/option/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
/**
* # Option Module
* The Option Module is inspired by the Scala 3 Option type. This module primarily solves one of the common problems in programming - avoiding null and undefined values.
*
* The Option type is a container that encapsulates an optional value, i.e., it stores some value or none. It's a safer alternative to using null or undefined. By using Option, you can avoid null reference errors. The main advantage of using an `Option` type is that you're explicitly dealing with something that may or may not be there.
*
* ## How it works
* This module exports a base abstract class `Option` and two concrete subclasses `Some` and `None`. An `Option` object encapsulates a value that may or may not be present. A `Some` object represents an `Option` that contains a value, and `None` represents an `Option` that has no value.
*
* ## How to use it
* When you have a value that you want to lift into a boxed `Option`, you create an object of type `Some`, as follows:
*
* ```ts
* import { Option } from './option.ts'
*
* const value: Option.Type<string> = Option.Some("Hello, world!");
* ```
*
* If there isn't a value to lift, you create a `None` object, as follows:
*
* ```ts
* import { Option } from './option.ts'
*
* const none: Option.Type<never> = Option.None();
* ```
*
* To check the contents of an `Option`, use the `isSome` and `isNone` methods and extract the value when it is `Some`:
*
* ```ts
* import { Option } from './option.ts'
*
* const none: Option.Type<never> = Option.None();
*
* if (none.isSome()) {
* console.log(none.get()); // this will throw an error !!!
* } else {
* console.log("Value is None");
* }
* ```
*
* @module
*/

export { Option } from './option.ts'
Loading
Loading