• R/O
  • SSH

micro-namespace: Commit

javascript namespace library


Commit MetaInfo

Revisión6cb3341977ecae6b03c0cd84ae46170f6adda461 (tree)
Tiempo2017-12-15 19:44:41
Autor繧「繝ォ萓ソ <aru.bin@taky...>
Commiter繧「繝ォ萓ソ

Log Message

merge release/0.1.0

Cambiar Resumen

Diferencia incremental

diff -r 9076e6b7372f -r 6cb3341977ec Gruntfile.js
--- a/Gruntfile.js Fri Dec 15 13:33:53 2017 +0900
+++ b/Gruntfile.js Fri Dec 15 19:44:41 2017 +0900
@@ -31,6 +31,49 @@
3131 },
3232 testsuites :[],
3333 },
34+
35+ test :{
36+ options :{
37+ ////clearOnStart will have no effect
38+ ////if run inside grunt
39+ //clearOnStart :true,
40+ stopOnFail :true,
41+ stopOnException :true,
42+ verbose :true,
43+ outputColor :true,
44+ debugMode :true,
45+ useLogIcons :true,
46+ },
47+ testsuites :[
48+ {
49+ //name :"JxUnitTest",
50+ target :"./src/micro-namespace.js",
51+ file :"./test/micro-namespace.test.js",
52+ },
53+ ],
54+ },
55+
56+ testRelease :{
57+ options :{
58+ ////clearOnStart will have no effect
59+ ////if run inside grunt
60+ //clearOnStart :true,
61+ stopOnFail :true,
62+ stopOnException :true,
63+ verbose :true,
64+ outputColor :true,
65+ debugMode :true,
66+ useLogIcons :true,
67+ },
68+ testsuites :[
69+ {
70+ //name :"JxUnitTest",
71+ target :"./bin/micro-namespace/micro-namespace.js",
72+ file :"./test/micro-namespace.test.js",
73+ },
74+ ],
75+ },
76+
3477 },
3578
3679 jsDocConfig = {
@@ -139,11 +182,26 @@
139182 ],
140183 },
141184 {
185+ summary :"test beautiful code",
186+ target :"test",
187+ commands :[
188+ "jxunit:test",
189+ ],
190+ },
191+ {
192+ summary :"test uglified code",
193+ target :"test-release",
194+ commands :[
195+ "jxunit:testRelease",
196+ ],
197+ },
198+ {
142199 summary :"prepare the src dir for publish",
143200 target :"release",
144201 commands :[
145202 "uglify:release",
146203 "copy:release",
204+ "jxunit:testRelease",
147205 //"jsdoc:sable",
148206 ],
149207 },
diff -r 9076e6b7372f -r 6cb3341977ec README.md
--- a/README.md Fri Dec 15 13:33:53 2017 +0900
+++ b/README.md Fri Dec 15 19:44:41 2017 +0900
@@ -20,6 +20,12 @@
2020
2121 ## usage ##
2222
23+#### AMD require ####
24+
25+```javascript
26+var ns = require("micro-namespace");
27+```
28+
2329 #### include script ####
2430
2531 ```html
@@ -28,7 +34,7 @@
2834 ```
2935 #### examples ####
3036
31-simple namespace
37+**simple namespace**
3238
3339 ```javascript
3440 // create fb.common.util namespace
@@ -44,7 +50,7 @@
4450 }
4551 ```
4652
47-namespace and object
53+**namespace and object**
4854
4955 ```javascript
5056 // create fb.common.extra namespace with methods
@@ -61,4 +67,16 @@
6167
6268 var ax = new fb.common.extra.AjaxParser();
6369
70+```
71+
72+**amd require**
73+
74+```
75+var ns = require("micro-namespace");
76+
77+ns("fb.ajax", {
78+ getUsername :function(){ ... },
79+});
80+
81+var user = fb.ajax.getUsername();
6482 ```
\ No newline at end of file
diff -r 9076e6b7372f -r 6cb3341977ec src/micro-namespace.js
--- a/src/micro-namespace.js Fri Dec 15 13:33:53 2017 +0900
+++ b/src/micro-namespace.js Fri Dec 15 19:44:41 2017 +0900
@@ -29,7 +29,7 @@
2929 ⌐ ,⌐
3030
3131
32- micro-namespace 0.0.1
32+ micro-namespace 0.1.0
3333 Copyright 2017 Frostbane Ac
3434 2017.12.14
3535
@@ -56,7 +56,7 @@
5656 "use strict";
5757
5858 if(typeof exports !== typeof undefined && global.exports !== exports){
59- module.exports.namespace = factory;
59+ module.exports = factory;
6060 }else{
6161 global.namespace = factory;
6262 }
@@ -64,24 +64,53 @@
6464 }(this, (function(){
6565 "use strict";
6666
67- var namespace = function(name, obj){
68-
67+ var getRoot = function(){
6968 var $this = typeof window !== typeof undefined ?
7069 window :
71- typeof root !== typeof undefined ?
72- root :
70+ typeof global !== typeof undefined ?
71+ global :
7372 this;
7473
74+ return $this;
75+ };
76+
77+ var isInvalidName = function(name){
78+ if(name === undefined || name === null)
79+ return true;
80+
81+ if(typeof name !== typeof "")
82+ return true;
83+
84+ if(name.trim() === "")
85+ return true;
86+
87+ return false;
88+ };
89+
90+ var createNs = function(name, $this){
7591 var nsArray = name.split(".");
7692 var nsObj = nsArray.reduce(function(o, n){
7793 o[n] = o[n] || {};
7894 return o[n];
7995 }, $this);
8096
97+ return nsObj;
98+ };
8199
100+ var isInvalidObj = function(obj){
82101 if(obj === undefined || obj === null)
83- return nsObj;
102+ return true;
84103
104+ if(typeof obj !== typeof {})
105+ return true;
106+
107+ if(obj instanceof Array)
108+ return true;
109+
110+ return false;
111+ };
112+
113+ var copyPublicProp = function(nsObj, obj){
85114 for(var p in obj){
86115 if(!obj.hasOwnProperty(p))
87116 continue;
@@ -90,11 +119,35 @@
90119 }
91120
92121 return nsObj;
122+ };
123+
124+ /**
125+ *
126+ * @param {String} name
127+ * @param {Object} [obj]
128+ *
129+ * @returns {Object}
130+ *
131+ */
132+ var namespace = function(name, obj){
133+ var $this = getRoot();
134+
135+ if(isInvalidName(name))
136+ return $this;
137+
138+ var nsObj = createNs(name, $this);
139+
140+ if(isInvalidObj(obj))
141+ return nsObj;
142+
143+ var nsObj = copyPublicProp(nsObj, obj);
144+
145+ return nsObj;
93146
94147 };
95148
96149
97- namespace.version = "0.0.1.0";
150+ namespace.version = "0.1.0.0";
98151
99152 return namespace;
100153
diff -r 9076e6b7372f -r 6cb3341977ec src/package.json
--- a/src/package.json Fri Dec 15 13:33:53 2017 +0900
+++ b/src/package.json Fri Dec 15 19:44:41 2017 +0900
@@ -1,10 +1,10 @@
11 {
22 "name" : "micro-namespace",
3- "version" : "v0.0.1",
4- "description" : "Namespace",
3+ "version" : "v0.1.0",
4+ "description" : "Wrap object into a namespace",
55 "repository" : {
6- "type": "hg",
7- "url" : "localhost/micro-namespace"
6+ "type": "mercurial",
7+ "url" : "https://frostbane@bitbucket.org/frostbane/micro-namespace"
88 },
99 "main" : "./micro-namespace.js",
1010 "license" : "SEE LICENSE IN LICENSE.md",
diff -r 9076e6b7372f -r 6cb3341977ec test/micro-namespace.test.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/micro-namespace.test.js Fri Dec 15 19:44:41 2017 +0900
@@ -0,0 +1,215 @@
1+/* jshint
2+maxlen: false
3+ */
4+
5+/* global
6+log,
7+warn,
8+error,
9+mark,
10+assertTrue,
11+assertFalse,
12+assertEqual,
13+assertNotEqual,
14+assertUndefined,
15+assertNotUndefined,
16+assertSame,
17+assertNotSame,
18+result,
19+MicroCheck
20+ */
21+
22+
23+/**
24+ * @augments JxUnit
25+ */
26+var micronstest = {
27+
28+
29+ /* */// ------------------------------------------------ //
30+ /* */// variables/helpers
31+ /* */// ------------------------------------------------ //
32+
33+ data :{
34+ },
35+
36+ getRoot :function(){
37+ var $this = typeof window !== typeof undefined ?
38+ window :
39+ typeof global !== typeof undefined ?
40+ global :
41+ this;
42+
43+ return $this;
44+ },
45+
46+ deleteNamespaceRoot :function(n){
47+ var $this = this.getRoot();
48+
49+ var rootns = n.split(".")[0];
50+
51+ if($this[rootns] !== undefined)
52+ $this[rootns] = undefined;
53+ },
54+
55+ /* */// ------------------------------------------------ //
56+ /* */// startUp, setUp
57+ /* */// ------------------------------------------------ //
58+
59+ startUp :function(){
60+ "use strict";
61+
62+ namespace("fb");
63+ this.deleteNamespaceRoot("fb");
64+ },
65+
66+ setUp :function(){
67+ "use strict";
68+
69+ this.deleteNamespaceRoot("fb");
70+ },
71+
72+ /* */// ------------------------------------------------ //
73+ /* */// test(s)
74+ /* */// ------------------------------------------------ //
75+
76+ test_simpleNamespace :function(){
77+ "use strict";
78+
79+ assertUndefined(fb);
80+
81+ namespace("fb.common.utility");
82+
83+ assertNotUndefined(fb);
84+ assertNotUndefined(fb.common);
85+ assertNotUndefined(fb.common.utility);
86+ },
87+
88+ test_undefined :function(){
89+ "use strict";
90+
91+ var ret = namespace();
92+ assertEqual(this.getRoot(), ret);
93+
94+ },
95+
96+ test_null :function(){
97+ "use strict";
98+
99+ var ret = namespace(null);
100+ assertEqual(this.getRoot(), ret);
101+
102+ },
103+
104+ test_empty :function(){
105+ "use strict";
106+
107+ var ret;
108+
109+ ret = namespace("");
110+ assertEqual(this.getRoot(), ret);
111+
112+ ret = namespace(" ");
113+ assertEqual(this.getRoot(), ret);
114+
115+ // wide space
116+ ret = namespace("      ");
117+ assertEqual(this.getRoot(), ret);
118+
119+ },
120+
121+ test_nonString :function(){
122+ "use strict";
123+
124+ var ret;
125+
126+ ret = namespace({});
127+ assertEqual(this.getRoot(), ret);
128+
129+ ret = namespace([]);
130+ assertEqual(this.getRoot(), ret);
131+
132+ ret = namespace(1);
133+ assertEqual(this.getRoot(), ret);
134+
135+ ret = namespace(1.2);
136+ assertEqual(this.getRoot(), ret);
137+
138+ ret = namespace(true);
139+ assertEqual(this.getRoot(), ret);
140+
141+ },
142+
143+ test_addNamespace :function(){
144+ "use strict";
145+
146+ assertUndefined(fb);
147+
148+ namespace("fb.test");
149+
150+ assertNotUndefined(fb.test);
151+
152+ namespace("fb.test.utility");
153+ namespace("fb.test.helper");
154+ namespace("fb.test.helper.assert");
155+ namespace("fb.common");
156+ namespace("fb.common.dom");
157+
158+ assertNotUndefined(fb.test.utility);
159+ assertNotUndefined(fb.test.helper);
160+ assertNotUndefined(fb.test.helper.assert);
161+ assertNotUndefined(fb.common);
162+ assertNotUndefined(fb.common.dom);
163+ },
164+
165+ test_nsObj :function(){
166+ "use strict";
167+
168+ assertUndefined(fb);
169+
170+ var myLogger = {
171+ debug :function(){},
172+ warning :function(){},
173+ error :function(){},
174+ fatal :function(){},
175+ info :function(){},
176+ };
177+
178+ var functionType = typeof (function(){});
179+
180+ namespace("fb.utility.log", myLogger);
181+
182+ assertNotUndefined(fb.utility.log);
183+ assertType(fb.utility.log.debug, functionType);
184+ assertType(fb.utility.log.warning, functionType);
185+ assertType(fb.utility.log.error, functionType);
186+ assertType(fb.utility.log.fatal, functionType);
187+ assertType(fb.utility.log.info, functionType);
188+ },
189+
190+ _test_template :function(){
191+ "use strict";
192+
193+ assertUndefined(fb);
194+
195+ namespace("fb");
196+
197+ assertNotUndefined(fb);
198+ },
199+
200+
201+ /* */// ------------------------------------------------ //
202+ /* */// tearDown, shutDown
203+ /* */// ------------------------------------------------ //
204+
205+ tearDown :function(){
206+
207+ },
208+
209+ shutDown :function(){
210+ "use strict";
211+
212+ result.log();
213+ },
214+
215+};
Show on old repository browser