From 29d8aa086cadf9931d9b5b97a222b111e819c81a Mon Sep 17 00:00:00 2001 From: robonen Date: Thu, 24 Oct 2024 07:29:37 +0700 Subject: [PATCH] refactor(packages/stdlib): update dir names and imports --- packages/stdlib/src/arrays/index.ts | 6 ++- .../getByPath/index.ts | 0 packages/stdlib/src/collections/index.ts | 1 + packages/stdlib/src/structs/stack/index.ts | 7 ++- .../stdlib/src/text/template/index.test.ts | 54 +++++++++---------- packages/stdlib/src/text/template/index.ts | 11 ++-- 6 files changed, 43 insertions(+), 36 deletions(-) rename packages/stdlib/src/{arrays => collections}/getByPath/index.ts (100%) create mode 100644 packages/stdlib/src/collections/index.ts diff --git a/packages/stdlib/src/arrays/index.ts b/packages/stdlib/src/arrays/index.ts index 64eab40..cf02dc8 100644 --- a/packages/stdlib/src/arrays/index.ts +++ b/packages/stdlib/src/arrays/index.ts @@ -1 +1,5 @@ -export * from './getByPath'; \ No newline at end of file +export * from './cluster'; +export * from './first'; +export * from './last'; +export * from './sum'; +export * from './unique'; \ No newline at end of file diff --git a/packages/stdlib/src/arrays/getByPath/index.ts b/packages/stdlib/src/collections/getByPath/index.ts similarity index 100% rename from packages/stdlib/src/arrays/getByPath/index.ts rename to packages/stdlib/src/collections/getByPath/index.ts diff --git a/packages/stdlib/src/collections/index.ts b/packages/stdlib/src/collections/index.ts new file mode 100644 index 0000000..64eab40 --- /dev/null +++ b/packages/stdlib/src/collections/index.ts @@ -0,0 +1 @@ +export * from './getByPath'; \ No newline at end of file diff --git a/packages/stdlib/src/structs/stack/index.ts b/packages/stdlib/src/structs/stack/index.ts index 72cee96..db48251 100644 --- a/packages/stdlib/src/structs/stack/index.ts +++ b/packages/stdlib/src/structs/stack/index.ts @@ -1,3 +1,6 @@ +import { last } from '../../arrays'; +import { isArray } from '../../types'; + export type StackOptions = { maxSize?: number; }; @@ -37,7 +40,7 @@ export class Stack implements Iterable, AsyncIterable { */ constructor(initialValues?: T[] | T, options?: StackOptions) { this.maxSize = options?.maxSize ?? Infinity; - this.stack = Array.isArray(initialValues) ? initialValues : initialValues ? [initialValues] : []; + this.stack = isArray(initialValues) ? initialValues : initialValues ? [initialValues] : []; } /** @@ -95,7 +98,7 @@ export class Stack implements Iterable, AsyncIterable { if (this.isEmpty) throw new RangeError('Stack is empty'); - return this.stack[this.stack.length - 1]; + return last(this.stack); } /** diff --git a/packages/stdlib/src/text/template/index.test.ts b/packages/stdlib/src/text/template/index.test.ts index 2103056..5678e95 100644 --- a/packages/stdlib/src/text/template/index.test.ts +++ b/packages/stdlib/src/text/template/index.test.ts @@ -1,36 +1,36 @@ import { describe, expect, it } from 'vitest'; import { templateObject } from '.'; -describe('templateObject', () => { - // it('replace template placeholders with corresponding values from args', () => { - // const template = 'Hello, {names.0}!'; - // const args = { names: ['John'] }; - // const result = templateObject(template, args); - // expect(result).toBe('Hello, John!'); - // }); +describe.todo('templateObject', () => { + it('replace template placeholders with corresponding values from args', () => { + const template = 'Hello, {names.0}!'; + const args = { names: ['John'] }; + const result = templateObject(template, args); + expect(result).toBe('Hello, John!'); + }); - // it('replace template placeholders with corresponding values from args', () => { - // const template = 'Hello, {name}!'; - // const args = { name: 'John' }; - // const result = templateObject(template, args); - // expect(result).toBe('Hello, John!'); - // }); + it('replace template placeholders with corresponding values from args', () => { + const template = 'Hello, {name}!'; + const args = { name: 'John' }; + const result = templateObject(template, args); + expect(result).toBe('Hello, John!'); + }); - // it('replace template placeholders with fallback value if corresponding value is undefined', () => { - // const template = 'Hello, {name}!'; - // const args = { age: 25 }; - // const fallback = 'Guest'; - // const result = templateObject(template, args, fallback); - // expect(result).toBe('Hello, Guest!'); - // }); + it('replace template placeholders with fallback value if corresponding value is undefined', () => { + const template = 'Hello, {name}!'; + const args = { age: 25 }; + const fallback = 'Guest'; + const result = templateObject(template, args, fallback); + expect(result).toBe('Hello, Guest!'); + }); - // it(' replace template placeholders with fallback value returned by fallback function if corresponding value is undefined', () => { - // const template = 'Hello, {name}!'; - // const args = { age: 25 }; - // const fallback = (key: string) => `Unknown ${key}`; - // const result = templateObject(template, args, fallback); - // expect(result).toBe('Hello, Unknown name!'); - // }); + it(' replace template placeholders with fallback value returned by fallback function if corresponding value is undefined', () => { + const template = 'Hello, {name}!'; + const args = { age: 25 }; + const fallback = (key: string) => `Unknown ${key}`; + const result = templateObject(template, args, fallback); + expect(result).toBe('Hello, Unknown name!'); + }); it('replace template placeholders with nested values from args', () => { const result = templateObject('Hello {{user.name}, your address {user.addresses.0.street}', { diff --git a/packages/stdlib/src/text/template/index.ts b/packages/stdlib/src/text/template/index.ts index 473cadd..7a47b7a 100644 --- a/packages/stdlib/src/text/template/index.ts +++ b/packages/stdlib/src/text/template/index.ts @@ -1,4 +1,4 @@ -import { getByPath, type Generate } from '../../arrays'; +import { getByPath, type Generate } from '../../collections'; import { isFunction } from '../../types'; /** @@ -56,11 +56,10 @@ export function templateObject