From b30f3e425e217ffe6356c3430dc505484ae102fa Mon Sep 17 00:00:00 2001 From: tada5hi Date: Sun, 4 Feb 2024 11:36:52 +0100 Subject: [PATCH] feat: rename main fn to singa --- README.MD | 47 ++++++++++++++++++++++++++++++++++++++--- src/module.ts | 5 +++-- src/types.ts | 29 ++++++++++++++++++++++--- test/unit/index.spec.ts | 12 +++++------ 4 files changed, 79 insertions(+), 14 deletions(-) diff --git a/README.MD b/README.MD index bce0c81..1c1433c 100644 --- a/README.MD +++ b/README.MD @@ -6,11 +6,12 @@ [![Known Vulnerabilities][snyk-src]][snyk-href] [![Conventional Commits][conventional-src]][conventional-href] -This is a package to create and manage singleton instances. +This is a tiny library to create and manage singleton instances. **Table of Contents** - [Installation](#installation) - [Usage](#usage) +- [Types](#types) - [Contributing](#contributing) - [License](#license) @@ -22,14 +23,18 @@ npm install singa --save ## Usage +The **singa** function returns an object of type [Singa](#singa), +with all input parameters, including name and factory, being entirely optional. + ```typescript -import { defineSingleton } from 'singa'; +import { singa } from 'singa'; class Foo { } -const singleton = defineSingleton({ +const singleton = singa({ + name: 'singleton', factory() { return new Foo(); }, @@ -38,6 +43,42 @@ const singleton = defineSingleton({ const instance = singleton.use(); ``` +## Types + +### Singa +```typescript +declare type Singa = { + /** + * Create or us existing singleton instance. + */ + use: () => T, + /** + * Set the singleton instance. + * + * @param instance + */ + set: (instance: T) => void, + /** + * Set factory fn for instance creation. + * + * @param factory + */ + setFactory: (factory: Factory) => void, + /** + * Reset the singleton instance. + */ + reset: () => void, + /** + * Check if the singleton instance is set. + */ + has: () => boolean, + /** + * Check if a factory fn is set. + */ + hasFactory: () => boolean +}; +``` + ## Contributing Before starting to work on a pull request, it is important to review the guidelines for diff --git a/src/module.ts b/src/module.ts index de3c536..eec5b26 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,6 +1,6 @@ -import type { Factory, Singleton, SingletonCreateContext } from './types'; +import type { Factory, Singa, SingaCreateContext } from './types'; -export function defineSingleton(context: SingletonCreateContext = {}) : Singleton { +export function singa(context: SingaCreateContext = {}) : Singa { let instance : T | undefined; let factory : Factory | undefined; @@ -28,6 +28,7 @@ export function defineSingleton(context: SingletonCreateContext = {} factory = input; }, has: () => typeof instance !== 'undefined', + hasFactory: () => typeof factory !== 'undefined', reset: () => { instance = undefined; }, diff --git a/src/types.ts b/src/types.ts index 31d8738..aa5061d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,14 +1,37 @@ export type Factory = () => T; -export type SingletonCreateContext = { +export type SingaCreateContext = { factory?: Factory, name?: string }; -export type Singleton = { +export type Singa = { + /** + * Create or us existing singleton instance. + */ use: () => T, + /** + * Set the singleton instance. + * + * @param instance + */ set: (instance: T) => void, + /** + * Set factory fn for instance creation. + * + * @param factory + */ setFactory: (factory: Factory) => void, + /** + * Reset the singleton instance. + */ reset: () => void, - has: () => boolean + /** + * Check if the singleton instance is set. + */ + has: () => boolean, + /** + * Check if a factory fn is set. + */ + hasFactory: () => boolean }; diff --git a/test/unit/index.spec.ts b/test/unit/index.spec.ts index 46b060c..25319b1 100644 --- a/test/unit/index.spec.ts +++ b/test/unit/index.spec.ts @@ -1,4 +1,4 @@ -import { defineSingleton } from '../../src'; +import { singa } from '../../src'; class Foo { @@ -6,7 +6,7 @@ class Foo { describe('src/index.ts', () => { it('should create singleton', () => { - const singleton = defineSingleton({ + const singleton = singa({ factory() { return new Foo(); }, @@ -18,7 +18,7 @@ describe('src/index.ts', () => { }); it('should set singleton', () => { - const singleton = defineSingleton(); + const singleton = singa(); expect(singleton.has()).toBeFalsy(); singleton.set(new Foo()); expect(singleton.use()).toBeInstanceOf(Foo); @@ -26,7 +26,7 @@ describe('src/index.ts', () => { }); it('should reset singleton', () => { - const singleton = defineSingleton({ + const singleton = singa({ factory() { return new Foo(); }, @@ -40,7 +40,7 @@ describe('src/index.ts', () => { }); it('should set factory', () => { - const singleton = defineSingleton(); + const singleton = singa(); expect(singleton.has()).toBeFalsy(); singleton.setFactory(() => new Foo()); @@ -49,7 +49,7 @@ describe('src/index.ts', () => { }); it('should throw error', () => { - const singleton = defineSingleton(); + const singleton = singa(); try { singleton.use();