-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
459 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,23 @@ | ||
# std | ||
Types and utility functions brokered with **ts-blocks**. | ||
|
||
Types and utility functions brokered with ts-blocks. | ||
|
||
```bash | ||
npx ts-blocks@next add --repo https://github.com/ieedan/std | ||
``` | ||
pnpm dlx ts-blocks@next add --repo https://github.com/ieedan/std | ||
``` | ||
|
||
# Blocks | ||
|
||
| Category | Block | Status | | ||
| --------- | ------------ | -------------------------------------------------------------------------- | | ||
| types | result | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | array-sum | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | array-to-map | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | dispatcher | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | ipv4-address | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | is-number | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | map-to-array | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | pad | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | sleep | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | stopwatch | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | | ||
| utilities | truncate | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,73 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { formatToString, parse, validate } from './ipv4-address'; | ||
import { describe, expect, it } from "vitest"; | ||
import { formatToString, parse, validate } from "./ipv4-address"; | ||
|
||
describe('parse', () => { | ||
describe("parse", () => { | ||
it(`Allows '.' separator.`, () => { | ||
const expected = [192, 168, 100, 10]; | ||
|
||
expect(parse(expected.join('.')).unwrap()).toStrictEqual(expected); | ||
expect(parse(expected.join(".")).unwrap()).toStrictEqual(expected); | ||
}); | ||
|
||
it(`Allows '_' separator.`, () => { | ||
const expected = [192, 168, 100, 10]; | ||
|
||
expect(parse(expected.join('_')).unwrap()).toStrictEqual(expected); | ||
expect(parse(expected.join("_")).unwrap()).toStrictEqual(expected); | ||
}); | ||
|
||
it(`Allows ' ' separator.`, () => { | ||
const expected = [192, 168, 100, 10]; | ||
|
||
expect(parse(expected.join(' ')).unwrap()).toStrictEqual(expected); | ||
expect(parse(expected.join(" ")).unwrap()).toStrictEqual(expected); | ||
}); | ||
|
||
it('Allows leading 0s', () => { | ||
expect(parse('001.001.001.001').unwrap()).toStrictEqual([1, 1, 1, 1]); | ||
it("Does not allow non numbers.", () => { | ||
expect(parse("a.a.a.a").unwrapErr()).toStrictEqual({ | ||
message: "'a' is not a number.", | ||
octet: 1, | ||
}); | ||
}); | ||
|
||
it("Allows leading 0s", () => { | ||
expect(parse("001.001.001.001").unwrap()).toStrictEqual([1, 1, 1, 1]); | ||
}); | ||
|
||
it('Returns error when invalid', () => { | ||
expect(parse('192.168.256.10').unwrapErr()).toStrictEqual({ | ||
it("Returns error when invalid", () => { | ||
expect(parse("192.168.256.10").unwrapErr()).toStrictEqual({ | ||
octet: 3, | ||
message: "'256' is out of range.", | ||
}); | ||
expect(parse('192.168.0').unwrapErr()).toStrictEqual({ | ||
expect(parse("192.168.0").unwrapErr()).toStrictEqual({ | ||
message: "'192.168.0' is invalid as it doesn't contain 4 octets.", | ||
}); | ||
}); | ||
}); | ||
|
||
describe('validate', () => { | ||
it('Returns true when valid', () => { | ||
describe("validate", () => { | ||
it("Returns true when valid", () => { | ||
expect(validate([192, 168, 100, 10])).toBe(true); | ||
expect(validate('192.168.100.10')).toBe(true); | ||
expect(validate('192_168_100_10')).toBe(true); | ||
expect(validate("192.168.100.10")).toBe(true); | ||
expect(validate("192_168_100_10")).toBe(true); | ||
}); | ||
|
||
it('Returns false when invalid', () => { | ||
it("Returns false when invalid", () => { | ||
expect(validate([192, 168, 100, -10])).toBe(false); | ||
expect(validate([192, 168, 100, -56])).toBe(false); | ||
expect(validate('192.168.100.256')).toBe(false); | ||
expect(validate('192.168.100.-10')).toBe(false); | ||
expect(validate("192.168.100.256")).toBe(false); | ||
expect(validate("192.168.100.-10")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('formatToString', () => { | ||
it('Returns the correct format', () => { | ||
expect(formatToString([192, 168, 100, 10]).unwrap()).toBe('192.168.100.10'); | ||
expect(formatToString([192, 168, 100, 10], ' ').unwrap()).toBe('192 168 100 10'); | ||
expect(formatToString([192, 168, 100, 10], '_').unwrap()).toBe('192_168_100_10'); | ||
describe("formatToString", () => { | ||
it("Returns the correct format", () => { | ||
expect(formatToString([192, 168, 100, 10]).unwrap()).toBe("192.168.100.10"); | ||
expect(formatToString([192, 168, 100, 10], " ").unwrap()).toBe("192 168 100 10"); | ||
expect(formatToString([192, 168, 100, 10], "_").unwrap()).toBe("192_168_100_10"); | ||
expect(formatToString("192.168.100.10", "_").unwrap()).toBe("192_168_100_10"); | ||
expect(formatToString("192 168 100 10", "_").unwrap()).toBe("192_168_100_10"); | ||
expect(formatToString("192_168_100_10", ".").unwrap()).toBe("192.168.100.10"); | ||
}); | ||
|
||
it("Returns error when invalid", () => { | ||
expect(formatToString("256.2.2.2").unwrapErr()).toBe("'256' is out of range."); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
coverage: { | ||
provider: "v8", | ||
reporter: ["json-summary"], | ||
exclude: ["node_modules/", "**/*.d.ts", "**/*.test.ts"], | ||
all: false, | ||
}, | ||
}, | ||
}); |