Core module of the mass framework in the backend

Source: Internet
Author: User

The front and back ends of the mass framework are built based on node. js. Different from the front-end, the backend depends on Some APIs provided by the node. js built-in module. Therefore, the front-end dom api modules dependent on the browser cannot be reused. Currently, the core module only has Lang, which can be used by flow, and more, such as random, Pinyin, base64, and UUID...

Node. js has a built-in module loading mechanism, which is very inconsistent with the apis I use on the front end. I will package it again. The core module and the front-end core module should be consistent in other methods. Of course, domready does not need to be removed from the backend. For example, because the V8 engine supports ecma262v5, the data type can be simplified. The biggest difference is that debug uses mass. log, and its second parameter indicates formatting and highlighting.

CodeIt is temporarily here, and various extensions will be made in the future!

(Function () {// backend part 2011.12.4 by situ zhengmei function mass () {} var version = 0.1, class2type = {"[object Global]": "Global ", "null": "null", "Nan": "Nan", "undefined": "undefined"}, tostring = class2type. tostring;/*** mix, add more members to an object * @ Param {object} target object * @ Param {object} source attribute package * @ return {object} target object */function mix (target, source) {var ARGs = []. slice. call (arguments), key, ride = Typeof ARGs [args. Length-1] = "Boolean "? Args. pop (): true; target = target | |{}; for (VAR I = 1; Source = ARGs [I ++];) {for (key in source) {If (ride |! (Key in target) {target [Key] = source [Key] ;}} return target ;}var rformat =/([\ D] + ?) /Ig; var formats = {bold: [1, 22], italic: [3, 23], underline: [4, 24], inverse: [7, 27], strike: [9, 29]}; var colors = {}; ['black', 'red', 'green', 'yellow', 'Blue ', 'magenta ', 'cyany', 'white']. foreach (function (word, I) {colors [word] = I}); colors. gray = 99; Function Format (ARR, STR) {return '\ x1b [' + arr [0] + 'M' + STR + '\ x1b [' + arr [1] + 'M';}; mix (mass, {// Add a member rword to the namespace object of this version :/[ ^,] +/G, V: version, "@ debug": True, root: process. CWD (),/*** array * @ Param {arraylike} the array object of the class to be processed by nodes * @ Param {number} start (optional. The starting subscript of the part to be extracted. If it is a negative number, * @ Param {number} end is optional. Specifies where to end the selection * @ return {array} */slice: function (nodes, start, end) {for (VAR I = 0, n = nodes. length, result = []; I <n; I ++) {result [I] = nodes [I];} If (arguments. length> 1) {return result. slice (START, (end | result. length) ;}else {return result ;}}, /*** is used to obtain the data type or determine the data type * @ Param {Any} OBJ: the object to be checked * @ Param {string} STR the type to be compared * @ return {string | Boolean} */type: function (OBJ, STR) {VaR Result = class2type [(OBJ = NULL | OBJ! = OBJ )? OBJ: tostring. call (OBJ)] | "#"; if (result. charat (0) = "#") {If (buffer. isbuffer (OBJ) {result = 'buffer'; // returns the constructor name} else {result = tostring. call (OBJ ). slice (8,-1) ;}}if (STR) {return STR === result;} return result ;}, /*** used for debugging * @ Param {string} s the content to be printed * @ Param {Boolean} color to highlight various colors. Use <code style = "Format: blod; color: red; Background: Green "> * The format value can be one of the five values in formats or their combination (separated by spaces). The background color and font color Can only be one of colors */log: function (s, color) {If (color) {S = S. replace (rformat, function (a, style, RET) {style. tolowercase (). split (";"). foreach (function (ARR) {arr = arr. split (":"); var type = arr [0]. trim (), val = arr [1]. trim (); Switch (type) {Case "format": Val. replace (/\ W +/g, function (Word) {If (formats [word]) {ret = format (formats [word], RET)}); break; case "background": Case "color": var array = type =" Color "? []: [] If (colors [Val]) {array [0] + = colors [Val] ret = format (array, RET )}}}); return ret;});} console. log (s) ;},/*** generates objects with unified key values for high-speed judgment * @ Param {array | string} if the array is a string, use ", "or separate spaces * @ Param {number} Val (Optional). The default value is 1 * @ return {object} */oneobject: function (array, Val) {If (typeof array = "string") {array = array. match (mass. rword) | [];} var result = {}, value = Val! = Void 0? VAL: 1; for (VAR I = 0, n = array. length; I <n; I ++) {result [array [I] = value;} return result ;}, mix: mix}); mass. noop = mass. error = function () {}; "Boolean, number, String, function, array, date, Regexp, arguments ". replace (mass. rword, function (name) {class2type ["[object" + name + "]"] = Name;}); var rmodule =/([^ (\ s] +) \(? ([^)] *) \)? /, Names = [], // list of module names to be processed rets ={}, // the return value CBI = 1e4 for the collection module; // generate the callback function name var map = mass ["@ modules"] ={}; // execute and remove all modules or callback functions with dependencies resolvecallbacks () {loop: For (VAR I = names. length, repeat, name; name = Names [-- I];) {var OBJ = map [name], deps = obj. deps; For (var key in deps) {If (deps. hasownproperty (key) & map [Key]. state! = 2) {continue loop; }}// if deps is an empty object or the status of the dependent module is 2 if (obj. State! = 2) {names. splice (I, 1); // you must remove it before executing var fn = obj. callback; rets [FN. _ name] = fn. apply (null, incarnate (obj. ARGs); // only collects the returned values of the module obj. state = 2; repeat = true ;}} repeat & resolvecallbacks () ;}function incarnate (ARGs) {// input a group of module names, returns the return value of the corresponding module for (VAR I = 0, ret = [], name; name = ARGs [I ++];) {ret. push (RETs [name]);} return ret;} function deferred () {// a simple asynchronous queuing var list = [], self = function (FN) {fn & FN. cal L & list. push (FN); return self;} self. method = "pop"; self. fire = function (FN) {While (fn = list [self. method] () {fn ();} return list. length? SELF: Self. complete ();} self. complete = mass. noop; return self;} var nativemodules = mass. oneobject ("assert, child_process, cluster, crypto, dgram, DNS," + "events, FS, HTTP, https, net, OS, path, querystring, Readline, REPL, TLS, tty, URL, util, Vm, zlib ") function usenativerequire (name, URL) {var Nick = Name. slice (1); If (nativemodules [Nick]) {map [name]. state = 2; rets [name] = require (Nick); resolvecallbacks ();} el Se {url = URL | mass. root + "/" + Nick + ". JS "; try {require (URL); resolvecallbacks ()} catch (e) {mass. stack (function ('mass. log ("\ 033 [31m' + E + '\ 033 [39 m")'); mass. stack. fire (); // print the error stack }}} mass. mix (mass, {Stack: deferred (), define: function (name, deps, callback) {// Module name, dependency list, module itself if (typeof deps = "function") {// callback = deps; deps = "";} callback when only two parameters are processed. _ name = "@" + name; // Module name this. require (deps, Callback) ;}, require: function (deps, callback, errback) {// dependency list, positive callback, negative callback VaR _ deps ={}, argS = [], DN = 0, Cn = 0; (deps + ""). replace (mass. rword, function (URL, name, match) {DN ++; match = URL. match (rmodule); name = "@" + match [1]; // obtain the module name if (! Map [name]) {// prevents repeated node generation and request map [name] ={}; // state: undefined, not loaded; 1 loaded; 2: usenativerequire (name, Match [2]) has been executed; // load module} else if (Map [name]. state = 2) {CN ++;} If (! _ Deps [name]) {args. push (name); _ deps [name] = "situ zhengmei"; // deduplication}); var cbname = callback. _ name; If (DN = cn) {// If (cbname &&! (Cbname in RETs) {map [cbname]. state = 2 // If the merge method is used, the module will run into this branch (only once) return rets [cbname] = callback. apply (null, incarnate (ARGs);} else if (! Cbname) {// a normal callback can be executed for countless times. apply (null, incarnate (ARGs)} cbname = cbname | "@ CB" + (CBI ++ ). tostring (32); If (errback) {mass. stack (errback); // press into the error stack} map [cbname] = {// create or update the module status callback: callback, deps: _ deps, argS: ARGs, state: 1}; // normally, the module can only execute names through resolvecallbacks. unshift (cbname); resolvecallbacks () ;}}); exports. mass = Global. mass = mass ;})();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.