Skip to content

Commit

Permalink
chore(stdlib): Add examples to Char module (#1951)
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake authored Jan 26, 2024
1 parent f0f1f87 commit 1a948bb
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 9 deletions.
74 changes: 65 additions & 9 deletions stdlib/char.gr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
*
* @example include "char"
*
* @since 0.3.0
* @example 'a'
* @example '1'
* @example '🌾'
*
* @since v0.3.0
*/

module Char
Expand All @@ -19,13 +23,13 @@ exception MalformedUtf8
/**
* The minimum valid Unicode scalar value.
*
* @since 0.3.0
* @since v0.3.0
*/
provide let min = 0x0000
/**
* The maximum valid Unicode scalar value.
*
* @since 0.3.0
* @since v0.3.0
*/
provide let max = 0x10FFFF

Expand All @@ -35,7 +39,10 @@ provide let max = 0x10FFFF
* @param charCode: The number to check
* @returns `true` if the number refers to a valid Unicode scalar value or `false` otherwise
*
* @since 0.3.0
* @example Char.isValid(0) == true
* @example Char.isValid(-1) == false
*
* @since v0.3.0
*/
provide let isValid = charCode => {
charCode >= min &&
Expand All @@ -49,7 +56,10 @@ provide let isValid = charCode => {
* @param char: The character
* @returns The Unicode scalar value for the given character
*
* @since 0.3.0
* @example Char.code('a') == 97
* @example Char.code('🌾') == 127806
*
* @since v0.3.0
*/
@unsafe
provide let code = (char: Char) => {
Expand All @@ -68,7 +78,10 @@ provide let code = (char: Char) => {
*
* @throws InvalidArgument(String): When the Unicode scalar value is invalid
*
* @since 0.3.0
* @example Char.fromCode(97) == 'a'
* @example Char.fromCode(127806) == '🌾'
*
* @since v0.3.0
*/
@unsafe
provide let fromCode = (usv: Number) => {
Expand Down Expand Up @@ -99,7 +112,10 @@ provide let fromCode = (usv: Number) => {
*
* @throws Failure(String): When the input character is the maximum valid Unicode scalar value
*
* @since 0.3.0
* @example Char.succ('a') == 'b'
* @example Char.succ('1') == '2'
*
* @since v0.3.0
*/
provide let succ = char => {
let codePoint = code(char)
Expand All @@ -120,7 +136,10 @@ provide let succ = char => {
*
* @throws Failure(String): When the input character is the minimum valid Unicode scalar value
*
* @since 0.3.0
* @example Char.pred('b') == 'a'
* @example Char.pred('2') == '1'
*
* @since v0.3.0
*/
provide let pred = char => {
let codePoint = code(char)
Expand All @@ -139,7 +158,10 @@ provide let pred = char => {
* @param char: The character to convert
* @returns A string containing the given character
*
* @since 0.3.0
* @example Char.toString('a') == "a"
* @example Char.toString('🌾') == "🌾"
*
* @since v0.3.0
*/
@unsafe
provide let toString = (char: Char) => {
Expand Down Expand Up @@ -198,6 +220,13 @@ provide let toString = (char: Char) => {
* @param y: The second character
* @returns `true` if the first character is less than the second character or `false` otherwise
*
* @example
* from Char use { (<) }
* assert 'a' < 'b'
* @example
* from Char use { (<) }
* assert '1' < '2'
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -215,6 +244,16 @@ provide let (<) = (x: Char, y: Char) => {
* @param y: The second character
* @returns `true` if the first character is less than or equal to the second character or `false` otherwise
*
* @example
* from Char use { (<=) }
* assert 'a' <= 'b'
* @example
* from Char use { (<=) }
* assert '1' <= '2'
* @example
* from Char use { (<=) }
* assert 'a' <= 'a'
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -232,6 +271,13 @@ provide let (<=) = (x: Char, y: Char) => {
* @param y: The second character
* @returns `true` if the first character is greater than the second character or `false` otherwise
*
* @example
* from Char use { (>) }
* assert 'b' > 'a'
* @example
* from Char use { (>) }
* assert '2' > '1'
*
* @since v0.6.0
*/
@unsafe
Expand All @@ -249,6 +295,16 @@ provide let (>) = (x: Char, y: Char) => {
* @param y: The second character
* @returns `true` if the first character is greater than or equal to the second character or `false` otherwise
*
* @example
* from Char use { (>=) }
* assert 'b' >= 'a'
* @example
* from Char use { (>=) }
* assert '2' >= '1'
* @example
* from Char use { (>=) }
* assert 'a' >= 'a'
*
* @since v0.6.0
*/
@unsafe
Expand Down
130 changes: 130 additions & 0 deletions stdlib/char.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ No other changes yet.
include "char"
```

```grain
'a'
```

```grain
'1'
```

```grain
'🌾'
```

## Values

Functions and constants included in the Char module.
Expand Down Expand Up @@ -70,6 +82,16 @@ Returns:
|----|-----------|
|`Bool`|`true` if the number refers to a valid Unicode scalar value or `false` otherwise|

Examples:

```grain
Char.isValid(0) == true
```

```grain
Char.isValid(-1) == false
```

### Char.**code**

<details disabled>
Expand All @@ -95,6 +117,16 @@ Returns:
|----|-----------|
|`Number`|The Unicode scalar value for the given character|

Examples:

```grain
Char.code('a') == 97
```

```grain
Char.code('🌾') == 127806
```

### Char.**fromCode**

<details disabled>
Expand Down Expand Up @@ -126,6 +158,16 @@ Throws:

* When the Unicode scalar value is invalid

Examples:

```grain
Char.fromCode(97) == 'a'
```

```grain
Char.fromCode(127806) == '🌾'
```

### Char.**succ**

<details disabled>
Expand Down Expand Up @@ -157,6 +199,16 @@ Throws:

* When the input character is the maximum valid Unicode scalar value

Examples:

```grain
Char.succ('a') == 'b'
```

```grain
Char.succ('1') == '2'
```

### Char.**pred**

<details disabled>
Expand Down Expand Up @@ -188,6 +240,16 @@ Throws:

* When the input character is the minimum valid Unicode scalar value

Examples:

```grain
Char.pred('b') == 'a'
```

```grain
Char.pred('2') == '1'
```

### Char.**toString**

<details disabled>
Expand All @@ -213,6 +275,16 @@ Returns:
|----|-----------|
|`String`|A string containing the given character|

Examples:

```grain
Char.toString('a') == "a"
```

```grain
Char.toString('🌾') == "🌾"
```

### Char.**(<)**

<details disabled>
Expand All @@ -239,6 +311,18 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first character is less than the second character or `false` otherwise|

Examples:

```grain
from Char use { (<) }
assert 'a' < 'b'
```

```grain
from Char use { (<) }
assert '1' < '2'
```

### Char.**(<=)**

<details disabled>
Expand All @@ -265,6 +349,23 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first character is less than or equal to the second character or `false` otherwise|

Examples:

```grain
from Char use { (<=) }
assert 'a' <= 'b'
```

```grain
from Char use { (<=) }
assert '1' <= '2'
```

```grain
from Char use { (<=) }
assert 'a' <= 'a'
```

### Char.**(>)**

<details disabled>
Expand All @@ -291,6 +392,18 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first character is greater than the second character or `false` otherwise|

Examples:

```grain
from Char use { (>) }
assert 'b' > 'a'
```

```grain
from Char use { (>) }
assert '2' > '1'
```

### Char.**(>=)**

<details disabled>
Expand All @@ -317,3 +430,20 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first character is greater than or equal to the second character or `false` otherwise|

Examples:

```grain
from Char use { (>=) }
assert 'b' >= 'a'
```

```grain
from Char use { (>=) }
assert '2' >= '1'
```

```grain
from Char use { (>=) }
assert 'a' >= 'a'
```

0 comments on commit 1a948bb

Please sign in to comment.