mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
refactor(packages/stdlib): update dir names and imports
This commit is contained in:
@@ -1 +1,5 @@
|
||||
export * from './getByPath';
|
||||
export * from './cluster';
|
||||
export * from './first';
|
||||
export * from './last';
|
||||
export * from './sum';
|
||||
export * from './unique';
|
||||
1
packages/stdlib/src/collections/index.ts
Normal file
1
packages/stdlib/src/collections/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './getByPath';
|
||||
@@ -1,3 +1,6 @@
|
||||
import { last } from '../../arrays';
|
||||
import { isArray } from '../../types';
|
||||
|
||||
export type StackOptions = {
|
||||
maxSize?: number;
|
||||
};
|
||||
@@ -37,7 +40,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
||||
*/
|
||||
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<T> implements Iterable<T>, AsyncIterable<T> {
|
||||
if (this.isEmpty)
|
||||
throw new RangeError('Stack is empty');
|
||||
|
||||
return this.stack[this.stack.length - 1];
|
||||
return last(this.stack);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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}', {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getByPath, type Generate } from '../../arrays';
|
||||
import { getByPath, type Generate } from '../../collections';
|
||||
import { isFunction } from '../../types';
|
||||
|
||||
/**
|
||||
@@ -57,10 +57,9 @@ export function templateObject<T extends string, A extends Generate<ExtractPlace
|
||||
|
||||
// templateObject('Hello {user.name}, your address {user.addresses.0.street}', {
|
||||
// user: {
|
||||
// name: 'John Doe',
|
||||
// name: 'John',
|
||||
// addresses: [
|
||||
// { street: '123 Main St', city: 'Springfield'},
|
||||
// { street: '456 Elm St', city: 'Shelbyville'},
|
||||
// { city: 'New York', street: '5th Avenue' },
|
||||
// ],
|
||||
// },
|
||||
// });
|
||||
Reference in New Issue
Block a user