objectmakr
Advanced tools
Comparing version
@@ -11,8 +11,6 @@ define(["require", "exports", "./ObjectMakr"], function (require, exports, ObjectMakr_1) { | ||
*/ | ||
exports.stubObjectMakr = function (settings) { | ||
return new ObjectMakr_1.ObjectMakr(settings || { | ||
inheritance: {}, | ||
}); | ||
}; | ||
exports.stubObjectMakr = (settings) => new ObjectMakr_1.ObjectMakr(settings || { | ||
inheritance: {}, | ||
}); | ||
}); | ||
//# sourceMappingURL=fakes.js.map |
@@ -11,4 +11,4 @@ define(["require", "exports"], function (require, exports) { | ||
*/ | ||
var shallowCopy = function (recipient, donor) { | ||
for (var i in donor) { | ||
const shallowCopy = (recipient, donor) => { | ||
for (const i in donor) { | ||
if ({}.hasOwnProperty.call(donor, i)) { | ||
@@ -22,3 +22,3 @@ recipient[i] = donor[i]; | ||
*/ | ||
var ObjectMakr = /** @class */ (function () { | ||
class ObjectMakr { | ||
/** | ||
@@ -29,10 +29,8 @@ * Initializes a new instance of the ObjectMakr class. | ||
*/ | ||
function ObjectMakr(settings) { | ||
if (settings === void 0) { settings = {}; } | ||
constructor(settings = {}) { | ||
this.inheritance = settings.inheritance || {}; | ||
this.properties = settings.properties || {}; | ||
this.indexMap = | ||
settings.indexMap === undefined ? [] : settings.indexMap; | ||
this.indexMap = settings.indexMap === undefined ? [] : settings.indexMap; | ||
this.onMake = settings.onMake; | ||
this.classes = { Object: Object }; | ||
this.classes = { Object }; | ||
this.classParentNames = {}; | ||
@@ -48,6 +46,6 @@ this.generateClassParentNames(this.inheritance, "Object"); | ||
*/ | ||
ObjectMakr.prototype.getPrototypeOf = function (name) { | ||
getPrototypeOf(name) { | ||
this.ensureClassExists(name); | ||
return this.classes[name].prototype; | ||
}; | ||
} | ||
/** | ||
@@ -59,5 +57,5 @@ * Gets whether a class exists. | ||
*/ | ||
ObjectMakr.prototype.hasClass = function (name) { | ||
hasClass(name) { | ||
return name in this.classes || name in this.classParentNames; | ||
}; | ||
} | ||
/** | ||
@@ -71,5 +69,5 @@ * Creates a new instance of a class. | ||
*/ | ||
ObjectMakr.prototype.make = function (name, settings) { | ||
make(name, settings) { | ||
this.ensureClassExists(name); | ||
var instance = new this.classes[name](); | ||
const instance = new this.classes[name](); | ||
if (settings !== undefined) { | ||
@@ -82,3 +80,3 @@ shallowCopy(instance, settings); | ||
return instance; | ||
}; | ||
} | ||
/** | ||
@@ -90,9 +88,7 @@ * Creates a class from the recorded properties. | ||
*/ | ||
ObjectMakr.prototype.createClass = function (name) { | ||
var newClass = /** @class */ (function () { | ||
function newClass() { | ||
} | ||
return newClass; | ||
}()); | ||
var parentName = this.classParentNames[name]; | ||
createClass(name) { | ||
// It would be nice to declare this as a class { }, but the actual prototype will be readonly | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
const newClass = function () { }; | ||
const parentName = this.classParentNames[name]; | ||
if (parentName) { | ||
@@ -104,7 +100,7 @@ this.extendClass(newClass, parentName); | ||
} | ||
for (var i in this.properties[name]) { | ||
for (const i in this.properties[name]) { | ||
newClass.prototype[i] = this.properties[name][i]; | ||
} | ||
return newClass; | ||
}; | ||
} | ||
/** | ||
@@ -117,4 +113,4 @@ * Extends a class from a parent. | ||
*/ | ||
ObjectMakr.prototype.extendClass = function (newClass, parentName) { | ||
var parentClass = this.classes[parentName] | ||
extendClass(newClass, parentName) { | ||
const parentClass = this.classes[parentName] | ||
? this.classes[parentName] | ||
@@ -124,3 +120,3 @@ : this.createClass(parentName); | ||
newClass.prototype.constructor = newClass; | ||
}; | ||
} | ||
/** | ||
@@ -131,9 +127,9 @@ * Creates an output properties object with the mapping shown in indexMap | ||
*/ | ||
ObjectMakr.prototype.processIndexMappedProperties = function (shorthandProperties) { | ||
var output = {}; | ||
for (var i = 0; i < shorthandProperties.length; i += 1) { | ||
processIndexMappedProperties(shorthandProperties) { | ||
const output = {}; | ||
for (let i = 0; i < shorthandProperties.length; i += 1) { | ||
output[this.indexMap[i]] = shorthandProperties[i]; | ||
} | ||
return output; | ||
}; | ||
} | ||
/** | ||
@@ -145,8 +141,8 @@ * Recursively records the parent names for classes in an inheritance. | ||
*/ | ||
ObjectMakr.prototype.generateClassParentNames = function (inheritance, parentClassName) { | ||
for (var i in inheritance) { | ||
generateClassParentNames(inheritance, parentClassName) { | ||
for (const i in inheritance) { | ||
this.classParentNames[i] = parentClassName; | ||
this.generateClassParentNames(inheritance[i], i); | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -157,14 +153,13 @@ * Ensures a class exists. | ||
*/ | ||
ObjectMakr.prototype.ensureClassExists = function (name) { | ||
ensureClassExists(name) { | ||
if (!(name in this.classes)) { | ||
if (!(name in this.classParentNames)) { | ||
throw new Error("Unknown class name: '" + name + "'."); | ||
throw new Error(`Unknown class name: '${name}'.`); | ||
} | ||
this.classes[name] = this.createClass(name); | ||
} | ||
}; | ||
return ObjectMakr; | ||
}()); | ||
} | ||
} | ||
exports.ObjectMakr = ObjectMakr; | ||
}); | ||
//# sourceMappingURL=ObjectMakr.js.map |
@@ -12,3 +12,3 @@ { | ||
"devDependencies": { | ||
"shenanigans-manager": "^0.3.2" | ||
"shenanigans-manager": "^0.3.0" | ||
}, | ||
@@ -22,8 +22,10 @@ "license": "MIT", | ||
"scripts": { | ||
"clean": "rm -rf lib *.tsbuildinfo", | ||
"compile": "tsc -b", | ||
"hydrate": "node ../shenanigans-manager/bin/shenanigans-manager hydrate-package-json && yarn run test:setup", | ||
"hydrate": "yarn shenanigans-manager hydrate", | ||
"link": "yarn link", | ||
"publish:ci": "yarn shenanigans-manager publish-if-updated", | ||
"test": "yarn run test:setup && yarn run test:run", | ||
"test:run": "mocha-headless-chrome --file test/index.html", | ||
"test:setup": "node ../shenanigans-manager/bin/shenanigans-manager generate-tests" | ||
"test:setup": "yarn shenanigans-manager generate-tests" | ||
}, | ||
@@ -34,4 +36,3 @@ "shenanigans": { | ||
"types": "./lib/index.d.ts", | ||
"version": "0.7.9", | ||
"gitHead": "6379309b5ea381578fd84b96778fde081a937209" | ||
} | ||
"version": "0.8.0-beta0" | ||
} |
@@ -5,5 +5,6 @@ <!-- Top --> | ||
[](https://greenkeeper.io/) | ||
[](https://travis-ci.org/FullScreenShenanigans/ObjectMakr) | ||
[](https://prettier.io) | ||
 | ||
[](http://badge.fury.io/js/objectmakr) | ||
[](https://gitter.im/FullScreenShenanigans/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
@@ -146,28 +147,8 @@ An abstract factory for dynamic attribute-based classes. | ||
After [forking the repo from GitHub](https://help.github.com/articles/fork-a-repo/): | ||
This repository is a portion of the [EightBittr monorepo](https://raw.githubusercontent.com/FullScreenShenanigans/EightBittr). | ||
See its [docs/Development.md](../../docs/Development.md) for details on how to get started. 💖 | ||
``` | ||
git clone https://github.com/<your-name-here>/ObjectMakr | ||
cd ObjectMakr | ||
npm install | ||
yarn run setup | ||
yarn run verify | ||
``` | ||
### Running Tests | ||
- `yarn run setup` creates a few auto-generated setup files locally. | ||
- `yarn run verify` builds, lints, and runs tests. | ||
### Building | ||
```shell | ||
yarn run watch | ||
``` | ||
Source files are written under `src/` in TypeScript and compile in-place to JavaScript files. | ||
`yarn run watch` will directly run the TypeScript compiler on source files in watch mode. | ||
Use it in the background while developing to keep the compiled files up-to-date. | ||
#### Running Tests | ||
```shell | ||
yarn run test | ||
@@ -179,6 +160,7 @@ ``` | ||
Whenever you add, remove, or rename a `*.test.t*` file under `src/`, `watch` will re-run `yarn run test:setup` to regenerate the list of static test files in `test/index.html`. | ||
You can open that file in a browser to debug through the tests. | ||
You can open that file in a browser to debug through the tests, or run `yarn test:run` to run them in headless Chrome. | ||
<!-- Maps --> | ||
<!-- /Maps --> | ||
<!-- /Development --> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
126061
-14.46%22
-45%396
-50.19%164
-9.89%