@abdokouta/react-support
v2.1.0
Published
Laravel-style Str, Collection, and Registry utilities for JavaScript/TypeScript
Maintainers
Readme
✨ Features
- 🎯 Str Class - 100+ string manipulation methods matching Laravel's API
- 📦 Collection - Array collection with 50+ chainable methods (powered by collect.js)
- 🗺️ MapCollection - Map data structure with collection methods
- 🎲 SetCollection - Set data structure with collection methods
- 🏗️ BaseRegistry - Generic registry pattern for building extensible systems
- 🎭 Facades - Laravel-style facades for clean service access
- 💪 TypeScript - Full type safety with comprehensive type definitions
- 🔗 Chainable - Fluent, chainable API for elegant code
- 🚀 Zero Config - Works out of the box
📦 Installation
npm install @abdokouta/react-support
# or
pnpm add @abdokouta/react-support
# or
yarn add @abdokouta/react-support🚀 Usage
Str Class
import { Str } from '@abdokouta/react-support';
// String manipulation
Str.camel('foo_bar'); // 'fooBar'
Str.snake('fooBar'); // 'foo_bar'
Str.kebab('fooBar'); // 'foo-bar'
Str.studly('foo_bar'); // 'FooBar'
Str.title('a nice title'); // 'A Nice Title'
// String inspection
Str.contains('This is my name', 'my'); // true
Str.startsWith('Hello World', 'Hello'); // true
Str.endsWith('Hello World', 'World'); // true
Str.isJson('{"key": "value"}'); // true
Str.isUrl('https://example.com'); // true
Str.isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de'); // true
// String extraction
Str.after('This is my name', 'This is'); // ' my name'
Str.before('This is my name', 'my'); // 'This is '
Str.between('This is my name', 'This', 'name'); // ' is my '
// String modification
Str.limit('The quick brown fox', 10); // 'The quick...'
Str.slug('Laravel 5 Framework', '-'); // 'laravel-5-framework'
// And 80+ more methods!Collection (Array)
import { collect } from '@abdokouta/react-support';
// Create a collection
const collection = collect([1, 2, 3, 4, 5]);
// Chainable methods
collection
.filter((item) => item > 2)
.map((item) => item * 2)
.sum(); // 24
// Working with objects
const users = collect([
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Bob', age: 35 },
]);
users.where('age', '>', 25).pluck('name').all();
// ['John', 'Bob']
// Aggregation
collect([1, 2, 3, 4, 5]).sum(); // 15
collect([1, 2, 3, 4, 5]).avg(); // 3
collect([1, 2, 3, 4, 5]).max(); // 5
collect([1, 2, 3, 4, 5]).min(); // 1Facades
import { Facade, createFacade } from '@abdokouta/react-support';
import { Container } from '@abdokouta/ts-container';
// Set the container for facades
Facade.setFacadeContainer(Container.getContainer());
// Create a facade for a service
const Config = createFacade<IConfigService>('ConfigService');
// Use the facade
Config.get('app.name');
Config.set('app.debug', true);MapCollection
import { collectMap } from '@abdokouta/react-support';
const map = collectMap({ name: 'John', age: 30 });
map.set('city', 'New York');
map.get('name'); // 'John'
map.has('age'); // true
map.keys(); // ['name', 'age', 'city']
map.values(); // ['John', 30, 'New York']SetCollection
import { collectSet } from '@abdokouta/react-support';
const set1 = collectSet([1, 2, 3]);
const set2 = collectSet([2, 3, 4]);
set1.union(set2).all(); // [1, 2, 3, 4]
set1.intersect(set2).all(); // [2, 3]
set1.diff(set2).all(); // [1]BaseRegistry
import { BaseRegistry } from '@abdokouta/react-support';
// Create a typed registry
interface Theme {
name: string;
colors: Record<string, string>;
}
const themeRegistry = new BaseRegistry<Theme>({
validateBeforeAdd: (key, theme) => {
if (!theme.name) {
return { valid: false, error: 'Theme must have a name' };
}
return { valid: true };
},
afterAdd: (key, theme) => {
console.log(`Registered theme: ${theme.name}`);
},
});
// Register items
themeRegistry.register('dark', { name: 'Dark', colors: { bg: '#000' } });
themeRegistry.register('light', { name: 'Light', colors: { bg: '#fff' } });
// Retrieve items
const theme = themeRegistry.get('dark');
const allThemes = themeRegistry.getAll();
const hasTheme = themeRegistry.has('dark');📘 TypeScript Support
Full TypeScript support with comprehensive type definitions:
import {
Str,
Collection,
MapCollection,
SetCollection,
BaseRegistry,
} from '@abdokouta/react-support';
// Type-safe collections
const numbers: Collection<number> = collect([1, 2, 3]);
// Type-safe maps
const userMap: MapCollection<string, User> = collectMap();
// Type-safe sets
const tags: SetCollection<string> = collectSet(['tag1', 'tag2']);
// Type-safe registries
const registry = new BaseRegistry<MyType>();📄 License
MIT
🙏 Credits
- Inspired by Laravel's support package
- Array collections powered by collect.js
