diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..28591ea Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index b512c09..4f15dd1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules \ No newline at end of file +dist +node_modules +localgeneric*.tgz \ No newline at end of file diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 8215451..0000000 --- a/index.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Type definitions for localgeneric v1.1.0 -// Project: localgeneric -// Definitions by: Ryan Barr -declare module "localgeneric"; - -declare class _GenericStore { - public key: string; - - constructor(key: string, defaults?: T); - - public get(): T; - public set(v: T | Partial): void; -} - -type GenericStore = _GenericStore & T; - -export declare const Store: new (key: string, data?: T) => GenericStore; diff --git a/index.js b/index.ts similarity index 68% rename from index.js rename to index.ts index 70c737b..ecc8200 100644 --- a/index.js +++ b/index.ts @@ -1,6 +1,8 @@ -export class GenericStore { - constructor(key, defaults) { - const proto = { ...GenericStore.prototype }; +export class _GenericStore { + public key: string; + + constructor(key: string, defaults?: T) { + const proto = { ..._GenericStore.prototype }; if (defaults) Object.assign(proto, Object.getPrototypeOf(defaults)); Object.setPrototypeOf(this, proto); Object.assign(this, defaults); @@ -19,7 +21,7 @@ export class GenericStore { return data ? JSON.parse(data) : null; }; - set = (value) => { + set = (value: T | Partial) => { Object.assign(this, value); return window.localStorage.setItem( this.key, @@ -30,4 +32,7 @@ export class GenericStore { }; } -export const Store = GenericStore; +type GenericStore = _GenericStore & T; + +export const Store: new (key: string, data?: T) => GenericStore = + _GenericStore as any; diff --git a/package.json b/package.json index 877336c..12e5a6a 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,12 @@ "name": "localgeneric", "version": "1.1.3", "description": "Local Storage wrapper with TypeScript generics support.", - "main": "index.js", - "typings": "index.d.ts", + "main": "dist/index.js", + "types": "dist/index.d.ts", "scripts": { + "build": "tsc", "test": "jest --env=jsdom" }, - "files": [ - "index.ts" - ], "repository": { "type": "git", "url": "git+https://github.com/ryanbarr/localgeneric.git" diff --git a/tests/store.test.ts b/tests/store.test.ts index b87d14a..7c1ea2d 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -1,4 +1,4 @@ -import { Store } from "../index.js"; +import { Store } from "../index"; describe("string stores", () => { const store = new Store("stringStores", "J. Doe"); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..1b0fc4a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "lib": ["es2017", "es7", "es6", "dom"], + "declaration": true, + "outDir": "dist", + "strict": true, + "esModuleInterop": true + }, + "files": [ + "index.ts" + ], +} \ No newline at end of file