This commit is contained in:
2023-09-23 12:46:52 +00:00
parent 93686c4912
commit 9c9d9e9545
47 changed files with 84 additions and 657 deletions
+11
View File
@@ -0,0 +1,11 @@
import { combineObject } from "./combineObject.js";
export function combineArray<K extends string, V>(array: V[][], columnList: K[]): Record<K, V>[] {
const result: Record<K, V>[] = [];
for (const key of array.keys()) {
const item = array[key];
result[key] = combineObject(item, columnList);
}
return result;
}
@@ -0,0 +1,8 @@
export function combineObject<K extends string, V>(item: V[], columnList: K[]): Record<K, V> {
const newItem: Record<string, V> = {};
for (const columnIdx in columnList) {
const columnName = columnList[columnIdx];
newItem[columnName] = item[columnIdx];
}
return newItem;
}
@@ -0,0 +1,17 @@
import type { BytesLike } from "./BytesLike.js";
export function convertBytesLikeToArrayBuffer(data: BytesLike, encodeUTF8 = true): ArrayBuffer{
if (data instanceof ArrayBuffer) {
return data;
}
if (data instanceof Uint8Array) {
return data.buffer;
}
if (typeof(data) === 'string'){
if(encodeUTF8){
return (new TextEncoder()).encode(data);
}
return new Uint8Array(data.split('').map(s=>s.codePointAt(0) as number));
}
return data.buffer;
}
@@ -0,0 +1,17 @@
import type { BytesLike } from "./BytesLike.js";
export function convertBytesLikeToUint8Array(data: BytesLike, encodeUTF8 = true): Uint8Array {
if (data instanceof Uint8Array) {
return data;
}
if (data instanceof ArrayBuffer) {
return new Uint8Array(data);
}
if (typeof (data) === 'string') {
if(encodeUTF8){
return (new TextEncoder()).encode(data);
}
return new Uint8Array(data.split('').map(s=>s.codePointAt(0) as number));
}
return new Uint8Array(data.buffer);
}
@@ -0,0 +1,9 @@
import type { IDItem } from "../types.js";
export function convertIDArray<T>(array: Iterable<T>): IDItem<T>[] {
const result: IDItem<T>[] = [];
for (const id of array) {
result.push({ id });
}
return result;
}
@@ -0,0 +1,10 @@
export function convertIterableToMap<T extends object, K extends keyof T, V extends T[K] & (string | number | symbol)>(
values: Iterable<T>,
key: K
): Map<V, T> {
const result = new Map<V, T>();
for (const obj of values) {
result.set(obj[key] as V, obj);
}
return result;
}
+6
View File
@@ -0,0 +1,6 @@
export * from "./combineObject.js";
export * from "./combineArray.js";
export * from "./convertBytesLikeToArrayBuffer.js";
export * from "./convertBytesLikeToUint8Array.js";
export * from "./convertIDArray.js";
export * from "./convertIterableToMap.js"
+18
View File
@@ -0,0 +1,18 @@
import { format, formatISO9075 } from 'date-fns';
//const DATE_TIME_FORMAT = 'yyyy-MM-dd HH:mm:ss';
const DATE_TIME_FORMAT_WITH_FRACTION = 'yyyy-MM-dd HH:mm:ss.SSS';
export function formatTime(time: Date, withFraction?:boolean): string;
export function formatTime(time: Date, format:string): string;
export function formatTime(time: Date, withFractionOrFormat:string|boolean = false): string {
if (typeof withFractionOrFormat === "string") {
return format(time, withFractionOrFormat);
}
else if(withFractionOrFormat){
return format(time, DATE_TIME_FORMAT_WITH_FRACTION);
}
else {
return formatISO9075(time);
}
}
+3
View File
@@ -0,0 +1,3 @@
export * from "./formatTime.js";
export * from "./parseTime.js";
export * from "./parseYearMonth.js";
+5
View File
@@ -0,0 +1,5 @@
import {parseISO} from 'date-fns';
export function parseTime(dateString: string): Date{
return parseISO(dateString);
}
@@ -0,0 +1,3 @@
export function parseYearMonth(yearMonth: number): [number, number] {
return [(yearMonth / 12) | 0, yearMonth % 12 + 1];
}
+20 -15
View File
@@ -4,6 +4,12 @@ export * from "./error.js"
export * from "./unwrap.js"
export * from "./types.js"
export * as converter from "./converter/index.js"
export * as datetime from "./datetime/index.js"
export * as korean from "./korean/index.js"
import type { WrappedBuffer } from "./types.js"
export function isEmail(addr: string): boolean {
return String(addr)
.toLowerCase()
@@ -23,12 +29,6 @@ export function calcBase64Len(length: number) {
return ((4 * length / 3) + 3) & ~3;
}
/** Buffer이지만 ts에서 타입 구분 편의를 제공 */
export interface WrappedBuffer extends Buffer {
/** 타입구분자. 항상 undefined일 것이다 */
_w_type?: string;
}
/** ArrayBuffer,Uint8Array,Buffer를 WrappedBuffer로 변환
*/
export function wrapBuffer<T extends WrappedBuffer>(buffer: BufferSource): T {
@@ -65,15 +65,6 @@ export function delay<T = undefined>(time: number, result?: T): Promise<T | void
);
}
/**
* base64 string에 내부 타입으로 WrappedBuffer를 보관한 형태
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export type Base64String<T extends Buffer> = string & {
/** 타입구분자. 항상 undefined일 것이다 */
_b_type?: T;
}
export function base64FromWrappedBuffer<T extends WrappedBuffer>(buffer: T | ArrayBuffer | Uint8Array): Base64String<T> {
return Buffer.from(buffer).toString('base64');
}
@@ -81,3 +72,17 @@ export function base64FromWrappedBuffer<T extends WrappedBuffer>(buffer: T | Arr
export function wrappedBufferFromBase64<T extends WrappedBuffer>(base64: Base64String<T>): T {
return Buffer.from(base64, 'base64') as T;
}
export function isBufferSource(obj: unknown): obj is BufferSource {
if (obj instanceof ArrayBuffer){
return true;
}
if('SharedArrayBuffer' in globalThis && obj instanceof SharedArrayBuffer){
return true;
}
if (ArrayBuffer.isView(obj)){
return true;
}
return false;
}
+101
View File
@@ -0,0 +1,101 @@
const convListLevel1: Record<string, Record<string, string>> = {
'ㄱ': {
'ㅅ': 'ㄳ',
},
'ㄴ': {
'ㅈ': 'ㄵ',
'ㅎ': 'ㄶ',
},
'ㄹ': {
'ㅂ': 'ㄼ',
'ㄱ': 'ㄺ',
'ㅅ': 'ㄽ',
'ㅁ': 'ㄻ',
'ㅎ': 'ㅀ',
'ㅌ': 'ㄾ',
'ㅍ': 'ㄿ',
},
'ㅂ': {
'ㅅ': 'ㅄ',
},
}
const convListLevel2: Record<string, Record<string, string>> = {
'ㄱ': {
'ㄱ': 'ㄲ',
'ㅅ': 'ㄳ',
},
'ㄴ': {
'ㅈ': 'ㄵ',
'ㅎ': 'ㄶ',
},
'ㄷ': {
'ㄷ': 'ㄸ',
},
'ㄹ': {
'ㅂ': 'ㄼ',
'ㄱ': 'ㄺ',
'ㅅ': 'ㄽ',
'ㅁ': 'ㄻ',
'ㅎ': 'ㅀ',
'ㅌ': 'ㄾ',
'ㅍ': 'ㄿ',
},
'ㅂ': {
'ㅂ': 'ㅃ',
'ㅅ': 'ㅄ',
},
'ㅅ': {
'ㅅ': 'ㅆ',
},
'ㅈ': {
'ㅈ': 'ㅉ',
}
}
function automata초성(text: string, convList: Record<string, Record<string, string>>): string{
const result: string[] = [];
let head: undefined | string = undefined;
for (const ch of text) {
if (head === undefined) {
if(!(ch in convList)){
result.push(ch);
continue;
}
head = ch;
continue;
}
const nextConv = convList[head];
if(ch in nextConv){
result.push(nextConv[ch]);
head = undefined;
continue;
}
result.push(head);
if(!(ch in convList)){
result.push(ch);
head = undefined;
continue;
}
head = ch;
}
if(head !== undefined){
result.push(head);
head = undefined;
}
return result.join('');
}
export function automata초성All(text: string): [string, string]{
return [automata초성(text, convListLevel1), automata초성(text, convListLevel2)];
}
export function automata초성Level1(text: string): string{
return automata초성(text, convListLevel1);
}
export function automata초성Level2(text: string): string {
return automata초성(text, convListLevel2);
}
@@ -0,0 +1,9 @@
import { automata초성All } from "./automata초성.js";
import { filter초성withAlphabet } from "./filter초성withAlphabet.js";
export function convertSearch초성(text: string): string[]{
const [filteredTextH, filteredTextA] = filter초성withAlphabet(text.replace(/\s+/g, ""));
const [filteredTextHL1, filteredTextHL2] = automata초성All(filteredTextH);
return [text, filteredTextA, filteredTextH, filteredTextHL1, filteredTextHL2];
}
+17
View File
@@ -0,0 +1,17 @@
export function filter초성(text: string): string {
const = [
"ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ",
"ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"
];
const result: string[] = [];
for (const char of text) {
const code = (char.codePointAt(0) ?? 0) - 44032;
if (0 <= code && code < 11172) {
result.push([~~(code / 588)]);
}
else {
result.push(char);
}
}
return result.join('');
}
@@ -0,0 +1,24 @@
export function filter초성withAlphabet(text: string): [string, string] {
const = [
"ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ",
"ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"
];
const alphabets = [
"r", "R", "s", "e", "E", "f", "a", "q", "Q",
"t", "T", "d", "w", "W", "c", "z", "x", "v", "g"
];
const resultH: string[] = [];
const resultA: string[] = [];
for (const char of text) {
const code = (char.codePointAt(0) ?? 0) - 44032;
if (0 <= code && code < 11172) {
resultH.push([~~(code / 588)]);
resultA.push(alphabets[~~(code / 588)]);
}
else {
resultH.push(char);
resultA.push(char);
}
}
return [resultH.join(''), resultA.join('')];
}
+4
View File
@@ -0,0 +1,4 @@
export * from "./automata초성.js";
export * from "./convertSearch초성.js"
export * from "./filter초성.js";
export * from "./filter초성withAlphabet.js";
+28 -1
View File
@@ -1 +1,28 @@
export type Nullable<T> = T | null | undefined;
export type Nullable<T> = T | null | undefined;
export declare type ValuesOf<T> = T[keyof T];
export type IDItem<T> = {
id: T;
};
/** Buffer이지만 ts에서 타입 구분 편의를 제공 */
export interface WrappedBuffer extends Buffer {
/** 타입구분자. 항상 undefined일 것이다 */
_w_type?: string;
}
export type BufferSource = ArrayBufferView | ArrayBuffer | SharedArrayBuffer;
export type BytesLike = BufferSource | string;
/**
* base64 string에 내부 타입으로 WrappedBuffer를 보관한 형태
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export type Base64String<T extends Buffer> = string & {
/** 타입구분자. 항상 undefined일 것이다 */
_b_type?: T;
}