Skip to content

Commit

Permalink
Merge pull request #154 from wentout/proto
Browse files Browse the repository at this point in the history
apply, call, bind prototype chain fix
  • Loading branch information
wentout authored Nov 9, 2023
2 parents 319b43c + 7121d31 commit f715f86
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
6 changes: 3 additions & 3 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ exports.lookup = function (TypeNestedPath) {
return types.lookup(TypeNestedPath);
};
const apply = function (entity, Constructor, args) {
const result = Constructor.apply(entity, args);
const result = new entity[Constructor.TypeName](...args);
return result;
};
exports.apply = apply;
const call = function (entity, Constructor, ...args) {
const result = Constructor.call(entity, ...args);
const result = new entity[Constructor.TypeName](...args);
return result;
};
exports.call = call;
const bind = function (entity, Constructor) {
return (...args) => {
const result = Constructor.call(entity, ...args);
const result = new entity[Constructor.TypeName](...args);
return result;
};
};
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mnemonica",
"version": "0.9.971",
"version": "0.9.972",
"description": "abstract technique that aids information retention : instance inheritance system",
"type": "commonjs",
"main": "./build/index.js",
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable new-cap */
'use strict';

import { TypeLookup, IDEF } from './types';
Expand Down Expand Up @@ -94,9 +95,10 @@ export const lookup = function (TypeNestedPath) {
export const apply = function <E extends object, T extends object, S extends Proto<E, T>> (entity: E, Constructor: IDEF<T>, args?: unknown[]): {
[key in keyof S]: S[key]
} {
// const result = Constructor.apply(entity, args);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const result = Constructor.apply(entity, args);
const result = new entity[ Constructor.TypeName ](...args);
return result;
};

Expand All @@ -105,17 +107,19 @@ export const call = function <E extends object, T extends object, S extends Prot
} {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const result = Constructor.call(entity, ...args);
// const result = Constructor.call(entity, ...args);
const result = new entity[ Constructor.TypeName ](...args);
return result;
};

export const bind = function <E extends object, T extends object, S extends Proto<E, T>> (entity: E, Constructor: IDEF<T>): (...args: unknown[]) => {
[key in keyof S]: S[key]
} {
return (...args) => {
// const result = Constructor.call(entity, ...args);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const result = Constructor.call(entity, ...args);
const result = new entity[ Constructor.TypeName ](...args);
return result;
};
};
Expand Down Expand Up @@ -171,3 +175,4 @@ export const errors = descriptors.ErrorsTypes;

export { utils } from './utils';
export { defineStackCleaner } from './utils';
/* eslint-enable new-cap */
4 changes: 4 additions & 0 deletions test/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const tests = (opts) => {
moreOver,
anotherDefaultTypesCollection,
someADTCInstance,
SubOfSomeADTCTypePre,
SubOfSomeADTCTypePost,
subOfSomeADTCInstanceA,
subOfSomeADTCInstanceC,
subOfSomeADTCInstanceB,
Expand Down Expand Up @@ -347,6 +349,8 @@ const tests = (opts) => {

it('apply & call works correctly', () => {

expect(SubOfSomeADTCTypePre.existentInstance).equal(someADTCInstance);
expect(SubOfSomeADTCTypePost.existentInstance).equal(someADTCInstance);
expect(subOfSomeADTCInstanceA.test).equal(123);
expect(subOfSomeADTCInstanceA.sub_test).equal(321);
expect(subOfSomeADTCInstanceA.args).deep.to.equal([ 1, 2, 3 ]);
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,27 @@ const SomeADTCType = adtcDefine('SomeADTCType', function () {

const someADTCInstance = new SomeADTCType();

let SubOfSomeADTCTypePre = null;
let SubOfSomeADTCTypePost = null;
const SubOfSomeADTCType = SomeADTCType.define('SubOfSomeADTCType', function (...args) {
this.sub_test = 321;
this.args = args;
});

SubOfSomeADTCType.registerHook('preCreation', (opts) => {
SubOfSomeADTCTypePre = opts;
});
SubOfSomeADTCType.registerHook('postCreation', (opts) => {
SubOfSomeADTCTypePost = opts;
});

// debugger;
const subOfSomeADTCInstanceA = apply(someADTCInstance, SubOfSomeADTCType, [ 1, 2, 3 ]);

// debugger;
const subOfSomeADTCInstanceC = call(someADTCInstance, SubOfSomeADTCType, 1, 2, 3);

// debugger;
const subOfSomeADTCInstanceB = bind(someADTCInstance, SubOfSomeADTCType)(1, 2, 3);


Expand Down Expand Up @@ -594,6 +606,8 @@ describe('Main Test', () => {
moreOver,
anotherDefaultTypesCollection,
someADTCInstance,
SubOfSomeADTCTypePre,
SubOfSomeADTCTypePost,
subOfSomeADTCInstanceA,
subOfSomeADTCInstanceC,
subOfSomeADTCInstanceB,
Expand Down

0 comments on commit f715f86

Please sign in to comment.