Skip to content

Commit

Permalink
update createDefaultCodec to handle literal types correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 19, 2025
1 parent d7e23a3 commit 127fd37
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Transcend Inc.",
"name": "@transcend-io/type-utils",
"description": "Small package containing useful typescript utilities.",
"version": "1.8.1",
"version": "1.8.2",
"homepage": "https://github.com/transcend-io/type-utils",
"repository": {
"type": "git",
Expand Down
9 changes: 9 additions & 0 deletions src/codecTools/createDefaultCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ import * as t from 'io-ts';
export const createDefaultCodec = <C extends t.Mixed>(
codec: C,
): t.TypeOf<C> => {
console.log({codecName: codec.name})

Check failure on line 44 in src/codecTools/createDefaultCodec.ts

View workflow job for this annotation

GitHub Actions / run-eslint

Unexpected console statement
// If the codec is an union
if (codec instanceof t.UnionType) {
console.log("union type")

Check failure on line 47 in src/codecTools/createDefaultCodec.ts

View workflow job for this annotation

GitHub Actions / run-eslint

Unexpected console statement
// The default for a union containing arrays is a default array
const arrayType = codec.types.find(
(type: any) => type instanceof t.ArrayType,
Expand All @@ -60,6 +62,7 @@ export const createDefaultCodec = <C extends t.Mixed>(
type instanceof t.ArrayType,
);
if (objectType) {
console.log("objectType")

Check failure on line 65 in src/codecTools/createDefaultCodec.ts

View workflow job for this annotation

GitHub Actions / run-eslint

Unexpected console statement
return createDefaultCodec(objectType);
}

Expand All @@ -68,6 +71,7 @@ export const createDefaultCodec = <C extends t.Mixed>(
(type: any) => type instanceof t.NullType || type.name === 'null',
);
if (hasNull) {
console.log("hasNull")

Check failure on line 74 in src/codecTools/createDefaultCodec.ts

View workflow job for this annotation

GitHub Actions / run-eslint

Unexpected console statement
return null as t.TypeOf<C>;
}

Expand Down Expand Up @@ -111,6 +115,11 @@ export const createDefaultCodec = <C extends t.Mixed>(
) as t.TypeOf<C>;
}

// The default of a literal type is its value
if (codec instanceof t.LiteralType) {
return codec.value as t.TypeOf<C>;
}

// Handle primitive and common types
switch (codec.name) {
case 'string':
Expand Down
8 changes: 7 additions & 1 deletion src/tests/createDefaultCodec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createDefaultCodec } from '../codecTools';

chai.use(deepEqualInAnyOrder);

describe('buildDefaultCodec', () => {
describe.only('buildDefaultCodec', () => {
it('should correctly build a default codec for null', () => {
const result = createDefaultCodec(t.null);
expect(result).to.equal(null);
Expand All @@ -32,6 +32,12 @@ describe('buildDefaultCodec', () => {
expect(result).to.equal('');
});

it('should correctly build a default codec for a union of literals', () => {
const codec = t.union([t.literal('A'), t.literal('B')]);
const defaultCodec = createDefaultCodec(codec);
expect(defaultCodec).to.equal('A');
});

it('should correctly build a default codec for a union with null', () => {
const result = createDefaultCodec(t.union([t.string, t.null]));
// should default to null if the union contains null
Expand Down

0 comments on commit 127fd37

Please sign in to comment.