From 7ff8622e5f976ca57b9c0ebac8cab051a7f3ab4d Mon Sep 17 00:00:00 2001 From: Denis Sikuler Date: Wed, 4 Sep 2013 22:11:58 +0400 Subject: [PATCH] Add Gruntfile and UMD distribution --- Gruntfile.js | 58 +++++++++++++++++++++++++++ History.md | 4 ++ README.md | 41 ++++++++++++++----- component.json | 2 +- dist/basespace.js | 93 +++++++++++++++++++++++++++++++++++++++++++ dist/basespace.min.js | 1 + index.js | 4 +- package.json | 8 +++- test/basespace.js | 6 +-- test/index.html | 2 +- 10 files changed, 202 insertions(+), 17 deletions(-) create mode 100644 Gruntfile.js create mode 100644 dist/basespace.js create mode 100644 dist/basespace.min.js diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..32cf057 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,58 @@ +module.exports = function(grunt) { + + // Configuration + grunt.initConfig({ + + jshint: { + files: ["*.js"], + + options: { + // Enforcing + bitwise: true, + camelcase: true, + curly: true, + eqeqeq: true, + immed: true, + latedef: true, + newcap: true, + noarg: true, + noempty: true, + nonew: true, + quotmark: true, + undef: true, + unused: true, + + // Environment + node: true + } + }, + + uglify: { + minify: { + src: "dist/basespace.js", + dest: "dist/basespace.min.js" + } + }, + + umd: { + dist: { + template: "unit", + src: "index.js", + dest: "dist/basespace.js", + objectToExport: "namespace", + globalAlias: "basespace" + } + } + + }); + + // Plugins + grunt.loadNpmTasks("grunt-contrib-jshint"); + grunt.loadNpmTasks("grunt-contrib-uglify"); + grunt.loadNpmTasks("grunt-umd"); + + // Tasks + grunt.registerTask("build", ["umd", "uglify"]); + grunt.registerTask("default", ["jshint"]); + grunt.registerTask("all", ["jshint", "build"]); +}; diff --git a/History.md b/History.md index e69de29..3600821 100644 --- a/History.md +++ b/History.md @@ -0,0 +1,4 @@ +### 0.0.2 / 2013-09-04 + +* add `Gruntfile` +* add UMD distribution `dist/basespace.js` and `dist/basespace.min.js` diff --git a/README.md b/README.md index 69272c1..110feff 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ Then: component install gamtiq/basespace +### AMD, <script> + +Use `dist/basespace.js` or `dist/basespace.min.js` (minified version). + ## Usage ### Node, Component @@ -25,18 +29,37 @@ Then: var ns = require("basespace"); ... -## Examples +### AMD - var app = { - space: ns.space - }; - ns(["model", "ui.dialog", "ui.list", "ui.list.cyclic", "util"], app); - app.space("ui.menu", "template"); - app.space("data").util = {...}; +```js +define(["path/to/dist/basespace.js"], function(ns) { + ... +}); +``` -## API +### <script> - var ns = require("basespace"); +```html + + +``` + +## Example + +```js +var app = { + space: ns.space +}; +ns(["model", "ui.dialog", "ui.list", "ui.list.cyclic", "util"], app); +app.space("ui.menu", "template"); +app.space("data").util = {...}; +``` + +## API ### ns(namespaces: Array|String, [context: Object], [value]) diff --git a/component.json b/component.json index 4ac97cf..3222016 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "basespace", "repo": "gamtiq/basespace", "description": "Functions to create namespaces inside objects", - "version": "0.0.1", + "version": "0.0.2", "keywords": [ "namespace", "ns", diff --git a/dist/basespace.js b/dist/basespace.js new file mode 100644 index 0000000..33fa1c4 --- /dev/null +++ b/dist/basespace.js @@ -0,0 +1,93 @@ + +(function(root, factory) { + if(typeof exports === 'object') { + module.exports = factory(require, exports, module); + } + else if(typeof define === 'function' && define.amd) { + define(['require', 'exports', 'module'], factory); + } + else { + var req = function(id) {return root[id];}, + exp = root, + mod = {exports: exp}; + root.basespace = factory(req, exp, mod); + } +}(this, function(require, exports, module) { +/** + * @module basespace + */ + +/** + * Create specified namespaces if they do not exist. + * + * @param {Array|String} names + * Namespaces that should be created. + * @param {Object} [context=global] + * An object inside of which namespaces will be created. Global object (i.e. window in browser) by default. + * @param {Any} [value={}] + * A value that will be assigned to a final field. {} by default. + * @return + * The value for the last name. undefined by default (if no names were passed). + * @alias module:basespace + */ +function namespace(names, context, value) { + /*jshint laxbreak:true*/ + var bLastValue = arguments.length > 2, + namePart, nI, nL, obj, sName, space; + if (names) { + if (typeof names === "string") { + names = [names]; + } + if (! context) { + context = (function() {return this;}).call(null); + } + if (! bLastValue) { + value = {}; + } + for (nI = 0, nL = names.length; nI < nL; nI++) { + namePart = names[nI].split("."); + space = context; + while (namePart.length) { + sName = namePart.shift(); + if (namePart.length) { + obj = space[sName]; + if (! obj || (typeof obj !== "object" && typeof obj !== "function")) { + obj = space[sName] = {}; + } + space = obj; + } + else { + space = ! (sName in space) || bLastValue + ? (space[sName] = value) + : space[sName]; + } + } + } + } + return space; +} + +/** + * Create specified namespaces inside this object if they do not exist. + *
+ * This function is a "wrap" for the following code: + *
+ * var namespace = require("basespace");
+ * namespace(arguments, this);
+ * 
+ * It can be transferred to an object to use as a method. + * + * @param {...String} name + * A namespace that should be created. + * @return + * The value for the last name. undefined by default (if no names were passed). + */ +namespace.space = function(name) { + /*jshint unused:vars*/ + return namespace(arguments, this); +}; + +module.exports = namespace; + + return namespace; +})); diff --git a/dist/basespace.min.js b/dist/basespace.min.js new file mode 100644 index 0000000..13b5b8d --- /dev/null +++ b/dist/basespace.min.js @@ -0,0 +1 @@ +!function(a,b){if("object"==typeof exports)module.exports=b(require,exports,module);else if("function"==typeof define&&define.amd)define(["require","exports","module"],b);else{var c=function(b){return a[b]},d=a,e={exports:d};a.basespace=b(c,d,e)}}(this,function(a,b,c){function d(a,b,c){var d,e,f,g,h,i,j=arguments.length>2;if(a)for("string"==typeof a&&(a=[a]),b||(b=function(){return this}.call(null)),j||(c={}),e=0,f=a.length;f>e;e++)for(d=a[e].split("."),i=b;d.length;)h=d.shift(),d.length?(g=i[h],(!g||"object"!=typeof g&&"function"!=typeof g)&&(g=i[h]={}),i=g):i=h in i&&!j?i[h]:i[h]=c;return i}return d.space=function(){return d(arguments,this)},c.exports=d,d}); \ No newline at end of file diff --git a/index.js b/index.js index bbaac28..51d94d9 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ * @alias module:basespace */ function namespace(names, context, value) { + /*jshint laxbreak:true*/ var bLastValue = arguments.length > 2, namePart, nI, nL, obj, sName, space; if (names) { @@ -49,7 +50,7 @@ function namespace(names, context, value) { } } return space; -}; +} /** * Create specified namespaces inside this object if they do not exist. @@ -67,6 +68,7 @@ function namespace(names, context, value) { * The value for the last name. undefined by default (if no names were passed). */ namespace.space = function(name) { + /*jshint unused:vars*/ return namespace(arguments, this); }; diff --git a/package.json b/package.json index 9c541a2..5d4799d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "basespace", - "version": "0.0.1", + "version": "0.0.2", "description": "Functions to create namespaces inside objects", "main": "index.js", "directories": { @@ -18,7 +18,11 @@ ], "devDependencies": { "mocha": "*", - "chai": "*" + "chai": "*", + "grunt": "~0.4.1", + "grunt-contrib-jshint": "~0.6.4", + "grunt-contrib-uglify": "~0.2.4", + "grunt-umd": "~1.3.0" }, "homepage": "https://github.com/gamtiq/basespace", "author": "Denis Sikuler", diff --git a/test/basespace.js b/test/basespace.js index aca308f..b2c030f 100644 --- a/test/basespace.js +++ b/test/basespace.js @@ -15,13 +15,13 @@ describe("basespace", function() { expect, ns; // node - if (typeof chai === "undefined") { + if (typeof basespace === "undefined") { ns = require("../index"); - expect = require("chai").expect; + expect = require("./lib/chai").expect; } // browser else { - ns = require("basespace"); + ns = basespace; expect = chai.expect; } diff --git a/test/index.html b/test/index.html index 5c04ec0..f7df791 100644 --- a/test/index.html +++ b/test/index.html @@ -6,7 +6,7 @@
- +