Skip to content

Commit

Permalink
Add Gruntfile and UMD distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
gamtiq committed Sep 4, 2013
1 parent fc1d48a commit 7ff8622
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 17 deletions.
58 changes: 58 additions & 0 deletions 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"]);
};
4 changes: 4 additions & 0 deletions 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`
41 changes: 32 additions & 9 deletions README.md
Expand Up @@ -18,25 +18,48 @@ Then:

component install gamtiq/basespace

### AMD, <script>

Use `dist/basespace.js` or `dist/basespace.min.js` (minified version).

## Usage

### Node, Component

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
<script type="text/javascript" src="path/to/dist/basespace.js"></script>
<script type="text/javascript">
// basespace is available via basespace field of window object
var ns = basespace;
...
</script>
```

## 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])

Expand Down
2 changes: 1 addition & 1 deletion component.json
Expand Up @@ -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",
Expand Down
93 changes: 93 additions & 0 deletions 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. <code>{}</code> by default.
* @return
* The value for the last name. <code>undefined</code> 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 <code>this</code> object if they do not exist.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var namespace = require("basespace");
* namespace(arguments, this);
* </pre></code>
* 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. <code>undefined</code> by default (if no names were passed).
*/
namespace.space = function(name) {
/*jshint unused:vars*/
return namespace(arguments, this);
};

module.exports = namespace;

return namespace;
}));
1 change: 1 addition & 0 deletions dist/basespace.min.js

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

4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -49,7 +50,7 @@ function namespace(names, context, value) {
}
}
return space;
};
}

/**
* Create specified namespaces inside <code>this</code> object if they do not exist.
Expand All @@ -67,6 +68,7 @@ function namespace(names, context, value) {
* The value for the last name. <code>undefined</code> by default (if no names were passed).
*/
namespace.space = function(name) {
/*jshint unused:vars*/
return namespace(arguments, this);
};

Expand Down
8 changes: 6 additions & 2 deletions 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": {
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions test/basespace.js
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.html
Expand Up @@ -6,7 +6,7 @@
</head>
<body>
<div id="mocha"></div>
<script src="../build/build.js"></script>
<script src="../dist/basespace.js"></script>
<script src="lib/mocha.js"></script>
<script src="lib/chai.js"></script>
<script>mocha.setup('bdd')</script>
Expand Down

0 comments on commit 7ff8622

Please sign in to comment.