Skip to content

Commit

Permalink
加强了Codec。
Browse files Browse the repository at this point in the history
  • Loading branch information
kalxd committed May 23, 2024
1 parent a8f7dc5 commit 647e11d
Showing 1 changed file with 54 additions and 16 deletions.
70 changes: 54 additions & 16 deletions src/codec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from "purify-ts/Codec";

import { Codec, GetType } from "purify-ts/Codec";
import * as FP from "purify-ts/Codec";

type CodecInterface = Record<string, Codec<any>>;

Expand Down Expand Up @@ -48,6 +47,20 @@ const toCamelCase = <T extends string>(input: T): ToCamelCase<T> => {
return `${prefix}${firstWord}${capitalWord}` as ToCamelCase<T>;
};

type ToCamelCaseProp<T> = {
[K in keyof T as ToCamelCase<T & string>]: T[K];
};

const toCamelCaseProp = <T>(prop: T): ToCamelCaseProp<T> => {
type Key = keyof ToCamelCaseProp<T>;
let result = {} as ToCamelCaseProp<T>;
for (const k in prop) {
const key = toCamelCase(k) as Key;
result[key] = prop[k] as any;
}
return result;
};

// _aAb => _a_ab
// aAb => a_ab
type ToSnakeCase<T extends string> = T extends `_${infer Rest}`
Expand All @@ -63,7 +76,7 @@ const splitCamelCase = (input: string): Array<string> => {
const xs = input.substring(1);
const index = xs.search(reg);
if (index === -1) {
return [x + input];
return [input];
}

const curWord = x + xs.substring(0, index);
Expand All @@ -87,24 +100,49 @@ const toSnakeCase = <T extends string>(input: T): ToSnakeCase<T> => {
return prefix + word as ToSnakeCase<T>;
};

console.log(toSnakeCase("__CamsYYouXixi"));

type ToCamelCaseCodec<T extends CodecInterface> = {
[K in keyof T as ToCamelCase<K & string>]: T[K]
type ToSnakeCaseProp<T> = {
[K in keyof T as ToSnakeCase<K & string>]: T[K];
};

/*
export const camelCaseCodec = <T extends CodecInterface>(prop: T): ToCamelCase<T> => {
const innserSnakeCodec = {} as ToCamelCaseCodec<T>;
const toSnakeCaseProp = <T>(prop: T): ToSnakeCaseProp<T> => {
type Key = keyof ToSnakeCaseProp<T>;
let result = {} as ToSnakeCaseProp<T>;
for (const k in prop) {
const snakeKey = 0;
const key = toSnakeCase(k) as Key;
result[key] = prop[k] as any;
}

return result;
};

const innerCodec = Codec.custom<ToCamelCaseCodec<T>>({
decode: input => {},
encode: self => {}
});
type ToCamelCaseCodec<T extends CodecInterface> = {
[K in keyof T as ToCamelCase<K & string>]: GetType<T[K]>
};

export const camelCaseCodec = <T extends CodecInterface>(prop: T): Codec<ToCamelCaseCodec<T>> => {
const snakeCodec = Codec.interface(toSnakeCaseProp(prop));

return Codec.custom<ToCamelCaseCodec<T>>({
decode: input => {
return snakeCodec.decode(input)
.map(obj => toCamelCaseProp(obj) as any);
},
encode: toSnakeCaseProp
});
};

};
*/
type ToSnakeCaseCodec<T extends CodecInterface> = {
[K in keyof T as ToSnakeCase<K & string>]: GetType<T[K]>;
};

export const snakeCaseCode = <T extends CodecInterface>(prop: T): Codec<ToSnakeCaseCodec<T>> => {
const camelCodec = Codec.interface(toCamelCaseProp(prop));

return Codec.custom<ToSnakeCaseCodec<T>>({
decode: input => {
return camelCodec.decode(input)
.map(obj => toSnakeCaseProp(obj) as any);
},
encode: toCamelCaseProp
});
};

0 comments on commit 647e11d

Please sign in to comment.