From c5e9b2063fba4f33baa3ee17089275fabc49e820 Mon Sep 17 00:00:00 2001 From: Egor Gorbachev Date: Wed, 27 May 2020 14:19:56 +0200 Subject: [PATCH] Add missing dependencies --- package.json | 14 +++++++- src/getter-definition-builder.ts | 2 +- src/interface-mapper.ts | 4 +-- src/module-definition-builder.ts | 2 +- src/utils/binder.ts | 55 ++++++++++++++++++++++++++++++++ src/utils/objects.ts | 44 +++++++++++++++++++++++++ yarn.lock | 23 +++++++++++++ 7 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 src/utils/binder.ts create mode 100644 src/utils/objects.ts create mode 100644 yarn.lock diff --git a/package.json b/package.json index 72e1bb6..2a0c17e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,19 @@ "name": "vuex-toolkit", "version": "0.0.1", "description": "Vuex Toolkit. An alternative API for Vuex.", - "main": "index.ts", + "main": "src/index.ts", + "dependencies": { + "@redux-saga/types": "^1.1.0", + "lodash": "^4.17.15" + }, + "peerDependencies": { + "vuex": "3.x.x", + "redux-saga": "1.x.x" + }, + "devDependencies": { + "@types/lodash": "^4.14.151", + "vuex": "3.x.x" + }, "repository": "git@github.com:banderror/vuex-toolkit.git", "author": "Georgii (Egor) Gorbachev ", "license": "MIT" diff --git a/src/getter-definition-builder.ts b/src/getter-definition-builder.ts index 21ecd60..ebeec56 100644 --- a/src/getter-definition-builder.ts +++ b/src/getter-definition-builder.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable no-param-reassign */ -import { Binder } from '../binder'; +import { Binder } from './utils/binder'; import { NamespaceInfo } from './namespace-info'; import { GetterNameDefinition, diff --git a/src/interface-mapper.ts b/src/interface-mapper.ts index c35a6f9..4ddbde8 100644 --- a/src/interface-mapper.ts +++ b/src/interface-mapper.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Store } from 'vuex'; -import { Saga } from 'redux-saga'; +import { Saga } from '@redux-saga/types'; import { MessageDefinition, MutationDefinition, @@ -12,7 +12,7 @@ import { isGetterDefinition, } from './base-types'; -import { isGeneratorFunction } from '../objects'; +import { isGeneratorFunction } from './utils/objects'; // --------------------------------------------------------------------------------------------------------------------- // API diff --git a/src/module-definition-builder.ts b/src/module-definition-builder.ts index dd2118b..dee1646 100644 --- a/src/module-definition-builder.ts +++ b/src/module-definition-builder.ts @@ -23,7 +23,7 @@ import { } from './base-types'; import { GetterDefinitionBuilder } from './getter-definition-builder'; -import { Binder } from '../binder'; +import { Binder } from './utils/binder'; interface MutationRegistration { definition: MutationDefinition; diff --git a/src/utils/binder.ts b/src/utils/binder.ts new file mode 100644 index 0000000..f97b8f6 --- /dev/null +++ b/src/utils/binder.ts @@ -0,0 +1,55 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +/** + * Returns an array with the names of the inherited enumerable properties of obj. + */ +function getEnumerableInheritedPropertyNames(obj) { + const result: any[] = []; + for (const propName in obj) { // eslint-disable-line no-restricted-syntax, guard-for-in + result.push(propName); + } + return result; +} + +/** + * Returns an array with the names of the inherited properties of obj. + */ +function getAllInheritedPropertyNames(obj) { + if ((typeof obj) !== 'object') { // null is not a problem + throw new Error('Only objects are allowed'); + } + + const props = {}; + + while (obj) { + const proto = Object.getPrototypeOf(obj); + + Object.getOwnPropertyNames(obj).forEach(p => { + props[p] = true; + }); + obj = proto; // eslint-disable-line no-param-reassign + } + + return Object.getOwnPropertyNames(props); +} + +export class Binder { + public static bindAllMethods(instance, cls?: Function) { + const methods = this.getAllMethods(instance, cls); + methods.forEach(mtd => { + // eslint-disable-next-line no-param-reassign + instance[mtd] = instance[mtd].bind(instance); + }); + } + + public static getAllMethods(instance, cls?: Function) { + const prototype = Object.getPrototypeOf(instance); + const ctor = cls || prototype.constructor; + + return getAllInheritedPropertyNames(instance) + .filter(name => { + const method = instance[name]; + return method instanceof Function && method !== ctor; + }); + } +} diff --git a/src/utils/objects.ts b/src/utils/objects.ts new file mode 100644 index 0000000..49a2733 --- /dev/null +++ b/src/utils/objects.ts @@ -0,0 +1,44 @@ +/** + * Check for plain object. + * + * @param {Mixed} val + * @return {Boolean} + */ +export function isObject(val) { + return val.constructor === Object; +} + +/** + * Check if `obj` is a promise. + * + * @param {Object} obj + * @return {Boolean} + */ +export function isPromise(obj) { + // eslint-disable-next-line eqeqeq + return typeof obj.then == 'function'; +} + +/** + * Check if `obj` is a generator. + * + * @param {Mixed} obj + * @return {Boolean} + */ +export function isGenerator(obj) { + // eslint-disable-next-line eqeqeq + return typeof obj.next == 'function' && typeof obj.throw == 'function'; +} + +/** + * Check if `obj` is a generator function. + * + * @param {Mixed} obj + * @return {Boolean} + */ +export function isGeneratorFunction(obj) { + const { constructor } = obj; + if (!constructor) return false; + if (constructor.name === 'GeneratorFunction' || constructor.displayName === 'GeneratorFunction') return true; + return isGenerator(constructor.prototype); +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..c0c87ef --- /dev/null +++ b/yarn.lock @@ -0,0 +1,23 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@redux-saga/types@^1.1.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@redux-saga/types/-/types-1.1.0.tgz#0e81ce56b4883b4b2a3001ebe1ab298b84237204" + integrity sha512-afmTuJrylUU/0OtqzaRkbyYFFNgCF73Bvel/sw90pvGrWIZ+vyoIJqA6eMSoA6+nb443kTmulmBtC9NerXboNg== + +"@types/lodash@^4.14.151": + version "4.14.153" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.153.tgz#5cb7dded0649f1df97938ac5ffc4f134e9e9df98" + integrity sha512-lYniGRiRfZf2gGAR9cfRC3Pi5+Q1ziJCKqPmjZocigrSJUVPWf7st1BtSJ8JOeK0FLXVndQ1IjUjTco9CXGo/Q== + +lodash@^4.17.15: + version "4.17.15" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548" + integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A== + +vuex@3.x.x: + version "3.4.0" + resolved "https://registry.yarnpkg.com/vuex/-/vuex-3.4.0.tgz#20cc086062d750769fce1febb34e7fceeaebde45" + integrity sha512-ajtqwEW/QhnrBZQsZxCLHThZZaa+Db45c92Asf46ZDXu6uHXgbfVuBaJ4gzD2r4UX0oMJHstFwd2r2HM4l8umg==