Skip to content

Commit

Permalink
Add missing dependencies [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
banderror committed May 18, 2020
1 parent 6f8b3b0 commit 81177e9
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 4 deletions.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
"name": "vuex-toolkit",
"version": "0.0.1",
"description": "Vuex Toolkit. An alternative API for Vuex.",
"main": "index.ts",
"main": "src/index.ts",
"dependencies": {
"lodash": "^4.17.15"
},
"peerDependencies": {
"vuex": "3.x.x"
},
"devDependencies": {
"@types/lodash": "^4.14.151"
},
"repository": "[email protected]:banderror/vuex-toolkit.git",
"author": "Georgii (Egor) Gorbachev <[email protected]>",
"license": "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/getter-definition-builder.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/interface-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isGetterDefinition,
} from './base-types';

import { isGeneratorFunction } from '../objects';
import { isGeneratorFunction } from './utils/objects';

// ---------------------------------------------------------------------------------------------------------------------
// API
Expand Down
2 changes: 1 addition & 1 deletion src/module-definition-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<S> {
definition: MutationDefinition<any>;
Expand Down
55 changes: 55 additions & 0 deletions src/utils/binder.ts
Original file line number Diff line number Diff line change
@@ -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;
});
}
}
44 changes: 44 additions & 0 deletions src/utils/objects.ts
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/lodash@^4.14.151":
version "4.14.151"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.151.tgz#7d58cac32bedb0ec37cb7f99094a167d6176c9d5"
integrity sha512-Zst90IcBX5wnwSu7CAS0vvJkTjTELY4ssKbHiTnGcJgi170uiS8yQDdc3v6S77bRqYQIN1App5a1Pc2lceE5/g==

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==

[email protected]:
version "3.4.0"
resolved "https://registry.yarnpkg.com/vuex/-/vuex-3.4.0.tgz#20cc086062d750769fce1febb34e7fceeaebde45"
integrity sha512-ajtqwEW/QhnrBZQsZxCLHThZZaa+Db45c92Asf46ZDXu6uHXgbfVuBaJ4gzD2r4UX0oMJHstFwd2r2HM4l8umg==

0 comments on commit 81177e9

Please sign in to comment.