Skip to content
Browse files

0.18.0

  • Loading branch information...
0 parents commit d8be6221e070b78ad98a96a6f00b6408045fdb69 @bvalosek committed Jan 13, 2014
Showing with 161 additions and 0 deletions.
  1. +21 −0 LICENSE
  2. 0 README.md
  3. +10 −0 index.js
  4. +32 −0 lib/extends.js
  5. +24 −0 lib/getArguments.js
  6. +29 −0 lib/getName.js
  7. +27 −0 lib/mixin.js
  8. +18 −0 package.json
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Brandon Valosek
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
0 README.md
No changes.
10 index.js
@@ -0,0 +1,10 @@
+module.exports = {
+
+ extends : require('./lib/extends.js'),
+ mixin : require('./lib/mixin.js'),
+ getArguments : require('./lib/getArguments.js'),
+ getName : require('./lib/getName.js')
+
+};
+
+
32 lib/extends.js
@@ -0,0 +1,32 @@
+module.exports = extends_;
+
+/**
+ * The well documented, oft-used (Coffeescript, Typescript, ES6... etc) extends
+ * pattern to get some sort of single-inheritance in Javascript. Modify a
+ * Child class to have inherited the static members via copying and link the
+ * prototypes.
+ * @param {Function} Child Child constructor function.
+ * @param {Function} Parent Parent contrusctor function.
+ * @return {Function} The Child constructor.
+ */
+function extends_(Child, Parent)
+{
+ // Drop in statics
+ for (var key in Parent) {
+ if (Parent.hasOwnProperty(key)) {
+ Child[key] = Parent[key];
+ }
+ }
+
+ // Give static to access parent
+ Child.Super = Parent;
+
+ // Child's prototype property is an object with the parent's prototype
+ // property its [[prototype]] + constructor
+ Child.prototype = Object.create(Parent.prototype, {
+ constructor: { value: Child }
+ });
+
+ return Child;
+}
+
24 lib/getArguments.js
@@ -0,0 +1,24 @@
+module.exports = getArguments;
+
+var FUNCTION_ARGS = /function[^\(]*\(([^\)]+)/;
+
+/**
+ * Get the parameter names of a function.
+ * @param {Function} f A function.
+ * @return {Array.<String>} An array of the argument names of a function.
+ */
+function getArguments(f)
+{
+ var ret = [];
+ var args = f.toString().match(FUNCTION_ARGS);
+
+ if (args) {
+ args[1].replace(/[ ]*,[ ]*/, ',')
+ .split(',')
+ .map(function(s) { return s.trim(); })
+ .forEach(function(a) { ret.push(a); }) ;
+ }
+
+ return ret;
+}
+
29 lib/getName.js
@@ -0,0 +1,29 @@
+module.exports = getName;
+
+var FUNCTION_NAME = /function\s+([^\s(]+)/;
+
+/**
+ * Get the name of a function (e.g. constructor)
+ * @param {Function} f
+ * @return {String} The function name.
+ */
+function getName(f)
+{
+ var name = '';
+
+ if (f instanceof Function) {
+ if (f.name) {
+ return f.name;
+ }
+
+ var match = f.toString().match(FUNCTION_NAME);
+
+ if (match) {
+ name = match[1];
+ }
+ } else if (f && f.constructor instanceof Function) {
+ name = getName(f.constructor);
+ }
+
+ return name;
+}
27 lib/mixin.js
@@ -0,0 +1,27 @@
+module.exports = mixin_;
+
+/**
+ * Add all own properties of mixin to the prototype property of class T
+ * @param {Function} T Class we want to mix into.
+ * @param {Function|Object} mixin Mixin we want to mixt
+ */
+function mixin_(T, mixin)
+{
+ // If we're mixing in a class (constructor function), then first mix in all
+ // things hanging directly off the mixin as "statics", then switch the mixin
+ // ref to point to the prototype
+ if (mixin instanceof Function) {
+ for (var k in mixin) {
+ T[k] = mixin[k];
+ }
+ mixin = mixin.prototype;
+ }
+
+ // Dump everything on the mixin into the prototype of our class
+ for (var key in mixin) {
+ if (mixin.hasOwnProperty(key)) {
+ T.prototype[key] = mixin[key];
+ }
+ }
+}
+
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "typedef",
+ "version": "0.18.0",
+ "description": "Utility functions for creating types in ES5 Javascript",
+ "directories": {
+ "test": "test"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git@github.com:bvalosek/typedef.git"
+ },
+ "author": "Brandon Valosek",
+ "license": "MIT",
+ "readmeFilename": "README.md"
+}

0 comments on commit d8be622

Please sign in to comment.
Something went wrong with that request. Please try again.