@makano/rew 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/rew +9 -0
- package/bin/ui +0 -0
- package/bin/webkit_app +0 -0
- package/lib/coffeescript/browser.js +151 -0
- package/lib/coffeescript/cake.js +135 -0
- package/lib/coffeescript/coffeescript.js +409 -0
- package/lib/coffeescript/command.js +750 -0
- package/lib/coffeescript/grammar.js +2496 -0
- package/lib/coffeescript/helpers.js +477 -0
- package/lib/coffeescript/index.js +217 -0
- package/lib/coffeescript/lexer.js +1943 -0
- package/lib/coffeescript/nodes.js +9204 -0
- package/lib/coffeescript/optparse.js +230 -0
- package/lib/coffeescript/parser.js +1344 -0
- package/lib/coffeescript/register.js +100 -0
- package/lib/coffeescript/repl.js +305 -0
- package/lib/coffeescript/rewriter.js +1138 -0
- package/lib/coffeescript/scope.js +187 -0
- package/lib/coffeescript/sourcemap.js +229 -0
- package/lib/rew/cli/cli.js +117 -0
- package/lib/rew/cli/log.js +40 -0
- package/lib/rew/cli/run.js +20 -0
- package/lib/rew/cli/utils.js +122 -0
- package/lib/rew/const/default.js +35 -0
- package/lib/rew/const/files.js +15 -0
- package/lib/rew/css/theme.css +3 -0
- package/lib/rew/functions/core.js +85 -0
- package/lib/rew/functions/emitter.js +57 -0
- package/lib/rew/functions/export.js +9 -0
- package/lib/rew/functions/future.js +22 -0
- package/lib/rew/functions/id.js +13 -0
- package/lib/rew/functions/import.js +57 -0
- package/lib/rew/functions/map.js +17 -0
- package/lib/rew/functions/match.js +34 -0
- package/lib/rew/functions/sleep.js +5 -0
- package/lib/rew/functions/types.js +96 -0
- package/lib/rew/html/ui.html +223 -0
- package/lib/rew/main.js +17 -0
- package/lib/rew/models/enum.js +14 -0
- package/lib/rew/models/struct.js +41 -0
- package/lib/rew/modules/compiler.js +17 -0
- package/lib/rew/modules/context.js +50 -0
- package/lib/rew/modules/fs.js +19 -0
- package/lib/rew/modules/runtime.js +24 -0
- package/lib/rew/modules/utils.js +0 -0
- package/lib/rew/modules/yaml.js +36 -0
- package/lib/rew/pkgs/conf.js +92 -0
- package/lib/rew/pkgs/data.js +8 -0
- package/lib/rew/pkgs/date.js +98 -0
- package/lib/rew/pkgs/modules/data/bintree.js +66 -0
- package/lib/rew/pkgs/modules/data/doublylinked.js +100 -0
- package/lib/rew/pkgs/modules/data/linkedList.js +88 -0
- package/lib/rew/pkgs/modules/data/queue.js +28 -0
- package/lib/rew/pkgs/modules/data/stack.js +27 -0
- package/lib/rew/pkgs/modules/ui/classes.js +171 -0
- package/lib/rew/pkgs/pkgs.js +13 -0
- package/lib/rew/pkgs/ui.js +108 -0
- package/package.json +41 -0
@@ -0,0 +1,122 @@
|
|
1
|
+
const path = require('path');
|
2
|
+
const fs = require('fs');
|
3
|
+
const conf = require('../pkgs/conf');
|
4
|
+
const jsYaml = require('js-yaml');
|
5
|
+
const readline = require('readline');
|
6
|
+
const { log, logget } = require('./log');
|
7
|
+
const { execSync } = require('child_process');
|
8
|
+
const { run } = require('../main');
|
9
|
+
|
10
|
+
const npm_package_name = '@kevinj045/rew';
|
11
|
+
|
12
|
+
module.exports = {
|
13
|
+
conf(command, fullPath, key, value){
|
14
|
+
const con = conf({});
|
15
|
+
if(command == 'get'){
|
16
|
+
if(!fullPath || fullPath == 'list'){
|
17
|
+
return fs.readdirSync(con.CONFIG_PATH).join('\n');
|
18
|
+
} else {
|
19
|
+
const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
|
20
|
+
const dpath = fullPath.indexOf('/') ? fullPath.split('/')[1] : '';
|
21
|
+
const root = con.create(name);
|
22
|
+
if(dpath){
|
23
|
+
const fp = path.join(root.root, dpath);
|
24
|
+
if(fs.existsSync(fp) && fs.statSync(fp).isDirectory()){
|
25
|
+
return fs.readdirSync(fp).join('\n');
|
26
|
+
} else {
|
27
|
+
const o = con.create(name).optionCenter(dpath);
|
28
|
+
return key ? o.get(key) : o.getAll(true);
|
29
|
+
}
|
30
|
+
} else {
|
31
|
+
return fs.readdirSync(root.root).join('\n');
|
32
|
+
}
|
33
|
+
}
|
34
|
+
} else {
|
35
|
+
const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
|
36
|
+
const dpath = fullPath.indexOf('/') ? fullPath.split('/')[1] : '';
|
37
|
+
if(name && key){
|
38
|
+
const root = con.create(name);
|
39
|
+
const o = dpath ? root.optionCenter(dpath) : root;
|
40
|
+
if(command == 'set') {
|
41
|
+
if(value){
|
42
|
+
o.set(key, value);
|
43
|
+
} else {
|
44
|
+
log('Value not specified', ':end');
|
45
|
+
}
|
46
|
+
} else {
|
47
|
+
o.remove(key);
|
48
|
+
}
|
49
|
+
} else {
|
50
|
+
log(
|
51
|
+
!name ? 'Path not specified' : 'Key not specified', ':end'
|
52
|
+
);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
},
|
56
|
+
createProject: (ppath) => {
|
57
|
+
const projectPath = path.join(process.cwd(), ppath);
|
58
|
+
log('Crating at', ppath);
|
59
|
+
const rl = readline.createInterface({
|
60
|
+
input: process.stdin,
|
61
|
+
output: process.stdout
|
62
|
+
});
|
63
|
+
const project = {};
|
64
|
+
const create = () => {
|
65
|
+
fs.mkdirSync(projectPath, { recursive: true });
|
66
|
+
const confPath = path.join(projectPath, 'app.yaml');
|
67
|
+
const entryFile = path.join(projectPath, 'main.coffee');
|
68
|
+
fs.writeFileSync(confPath, jsYaml.dump({ package: project.package, entry: 'main.coffee' }));
|
69
|
+
fs.writeFileSync(entryFile, `print("Hello World!")`);
|
70
|
+
if(project.git) {
|
71
|
+
execSync('cd '+projectPath+' && git init .');
|
72
|
+
}
|
73
|
+
log('Installing '+npm_package_name);
|
74
|
+
exec('cd '+projectPath+' && npm i '+npm_package_name, (err) => {
|
75
|
+
if(err){
|
76
|
+
console.error(err);
|
77
|
+
process.exit(0);
|
78
|
+
} else {
|
79
|
+
rl.close();
|
80
|
+
}
|
81
|
+
});
|
82
|
+
}
|
83
|
+
if (!fs.existsSync(projectPath)) {
|
84
|
+
rl.question(logget('Package Name: '), (pkg) => {
|
85
|
+
if(pkg.trim()) {
|
86
|
+
project.package = pkg.trim();
|
87
|
+
rl.question(logget('Use git(y/N): '), (use_git) => {
|
88
|
+
project.git = use_git.toLowerCase() == 'y' || use_git.toLowerCase() == 'yes';
|
89
|
+
create();
|
90
|
+
});
|
91
|
+
} else {
|
92
|
+
rl.close();
|
93
|
+
}
|
94
|
+
});
|
95
|
+
} else {
|
96
|
+
log(`Project ${ppath} already exists at ${projectPath}`, ':end');
|
97
|
+
rl.close();
|
98
|
+
}
|
99
|
+
},
|
100
|
+
runApp(pathOrPackage){
|
101
|
+
const apppath = path.resolve(process.cwd(), pathOrPackage);
|
102
|
+
const appConfpath = path.join(apppath, 'app.yaml');
|
103
|
+
|
104
|
+
const runAppRoot = (root, confPath) => {
|
105
|
+
const c = jsYaml.load(fs.readFileSync(confPath, { encoding: 'utf-8' }));
|
106
|
+
if(c.entry){
|
107
|
+
run(path.resolve(root, c.entry));
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
if(fs.existsSync(apppath) && fs.existsSync(appConfpath)){
|
112
|
+
runAppRoot(apppath, appConfpath);
|
113
|
+
} else {
|
114
|
+
const con = conf({});
|
115
|
+
const apppath = path.resolve(con.CONFIG_PATH, pathOrPackage, '$app');
|
116
|
+
const appConfpath = path.join(apppath, 'app.yaml');
|
117
|
+
if(fs.existsSync(apppath) && fs.existsSync(appConfpath)){
|
118
|
+
runAppRoot(apppath, appConfpath);
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
const { cenum } = require("../models/enum");
|
2
|
+
const { struct } = require("../models/struct");
|
3
|
+
const emitter = require("../functions/emitter");
|
4
|
+
const future = require("../functions/future");
|
5
|
+
const sleep = require("../functions/sleep");
|
6
|
+
const { match } = require("../functions/match");
|
7
|
+
const { map } = require("../functions/map");
|
8
|
+
const { typex, typeis, typedef, typei } = require("../functions/types");
|
9
|
+
const { isEmpty, clone, deepClone, merge, uniqueId, compose, curry } = require("../functions/core");
|
10
|
+
|
11
|
+
module.exports = {
|
12
|
+
cenum,
|
13
|
+
struct,
|
14
|
+
future,
|
15
|
+
emitter,
|
16
|
+
sleep,
|
17
|
+
match,
|
18
|
+
map,
|
19
|
+
typex,
|
20
|
+
typei,
|
21
|
+
typeis,
|
22
|
+
typedef,
|
23
|
+
|
24
|
+
isEmpty,
|
25
|
+
clone,
|
26
|
+
deepClone,
|
27
|
+
merge,
|
28
|
+
uniqueId,
|
29
|
+
compose,
|
30
|
+
curry,
|
31
|
+
|
32
|
+
print: function (...arguments) {
|
33
|
+
return console.log(...arguments);
|
34
|
+
},
|
35
|
+
};
|
@@ -0,0 +1,15 @@
|
|
1
|
+
const fs = require('fs');
|
2
|
+
const path = require('path');
|
3
|
+
|
4
|
+
const HOME_PATH = path.resolve(process.env.HOME, '.config/rew/default');
|
5
|
+
const THEME_PATH = module.exports.THEME_PATH = path.resolve(HOME_PATH, 'theme.css');
|
6
|
+
|
7
|
+
module.exports.FILES = [
|
8
|
+
{
|
9
|
+
path: HOME_PATH
|
10
|
+
},
|
11
|
+
{
|
12
|
+
path: THEME_PATH,
|
13
|
+
content: fs.readFileSync(path.resolve(__dirname, '../css/theme.css'))
|
14
|
+
}
|
15
|
+
]
|
@@ -0,0 +1,85 @@
|
|
1
|
+
|
2
|
+
function isEmpty(value) {
|
3
|
+
if (Array.isArray(value) || typeof value === 'string') {
|
4
|
+
return value.length === 0;
|
5
|
+
} else if (typeof value === 'object') {
|
6
|
+
return Object.keys(value).length === 0;
|
7
|
+
} else {
|
8
|
+
return true;
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
function clone(value) {
|
13
|
+
if (Array.isArray(value)) {
|
14
|
+
return value.slice();
|
15
|
+
} else if (typeof value === 'object') {
|
16
|
+
return Object.assign({}, value);
|
17
|
+
} else {
|
18
|
+
return value;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
function deepClone(value) {
|
23
|
+
if (Array.isArray(value)) {
|
24
|
+
return value.map(item => deepClone(item));
|
25
|
+
} else if (typeof value === 'object') {
|
26
|
+
const obj = {};
|
27
|
+
for (const key in value) {
|
28
|
+
if (value.hasOwnProperty(key)) {
|
29
|
+
obj[key] = deepClone(value[key]);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
return obj;
|
33
|
+
} else {
|
34
|
+
return value;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
function merge(obj1, obj2) {
|
39
|
+
return Object.assign({}, obj1, obj2);
|
40
|
+
}
|
41
|
+
|
42
|
+
const uniqueId = (() => {
|
43
|
+
let id = 0;
|
44
|
+
return () => {
|
45
|
+
id += 1;
|
46
|
+
return `id-${id}`;
|
47
|
+
};
|
48
|
+
})();
|
49
|
+
|
50
|
+
function filter(arr, fn) {
|
51
|
+
return arr.filter(fn);
|
52
|
+
}
|
53
|
+
|
54
|
+
function reduce(arr, fn, initial) {
|
55
|
+
return arr.reduce(fn, initial);
|
56
|
+
}
|
57
|
+
|
58
|
+
function compose(...fns) {
|
59
|
+
return initialValue => {
|
60
|
+
return fns.reduceRight((acc, fn) => fn(acc), initialValue);
|
61
|
+
};
|
62
|
+
}
|
63
|
+
|
64
|
+
function curry(fn) {
|
65
|
+
const curried = (...args) => {
|
66
|
+
if (args.length >= fn.length) {
|
67
|
+
return fn.apply(null, args);
|
68
|
+
} else {
|
69
|
+
return (...moreArgs) => curried.apply(null, args.concat(moreArgs));
|
70
|
+
}
|
71
|
+
};
|
72
|
+
return curried;
|
73
|
+
}
|
74
|
+
|
75
|
+
module.exports = {
|
76
|
+
isEmpty,
|
77
|
+
clone,
|
78
|
+
deepClone,
|
79
|
+
merge,
|
80
|
+
uniqueId,
|
81
|
+
filter,
|
82
|
+
reduce,
|
83
|
+
compose,
|
84
|
+
curry
|
85
|
+
};
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module.exports = function emitter() {
|
2
|
+
const events = [];
|
3
|
+
const on = (event, callback, props = {}) => {
|
4
|
+
const addEvent = (event) => events.push({ ...props, event, callback });
|
5
|
+
if (Array.isArray(event)) {
|
6
|
+
event.forEach(addEvent);
|
7
|
+
} else {
|
8
|
+
addEvent(event);
|
9
|
+
}
|
10
|
+
return listener;
|
11
|
+
};
|
12
|
+
const off = (event, callback, removable = null) => {
|
13
|
+
const rm = (event) => {
|
14
|
+
if(removable){
|
15
|
+
removable(event);
|
16
|
+
}
|
17
|
+
events.splice(events.indexOf(event), 1);
|
18
|
+
}
|
19
|
+
const rmEvent = (event) => {
|
20
|
+
if (callback) {
|
21
|
+
const _events = events.filter(({ callback: c }) => c == callback);
|
22
|
+
_events.forEach((e) => {
|
23
|
+
if (Array.isArray(event)) {
|
24
|
+
if (event.includes(e.event)) rm(e);
|
25
|
+
} else {
|
26
|
+
if (e.event == event) rm(e);
|
27
|
+
}
|
28
|
+
});
|
29
|
+
} else {
|
30
|
+
const _events = events.filter(({ event: e }) => e == event);
|
31
|
+
_events.forEach((e) => {
|
32
|
+
rm(e);
|
33
|
+
});
|
34
|
+
}
|
35
|
+
};
|
36
|
+
if (Array.isArray(event)) {
|
37
|
+
event.forEach(rmEvent);
|
38
|
+
} else {
|
39
|
+
rmEvent(event);
|
40
|
+
}
|
41
|
+
return listener;
|
42
|
+
};
|
43
|
+
const emit = (event, ...data) => {
|
44
|
+
const emitEvent = (event) =>
|
45
|
+
events
|
46
|
+
.filter(({ event: e }) => e == event)
|
47
|
+
.forEach(({ callback }) => callback(...data));
|
48
|
+
if (Array.isArray(event)) {
|
49
|
+
event.forEach(emitEvent);
|
50
|
+
} else {
|
51
|
+
emitEvent(event);
|
52
|
+
}
|
53
|
+
return listener;
|
54
|
+
};
|
55
|
+
const listener = { on, off, emit };
|
56
|
+
return listener;
|
57
|
+
};
|
@@ -0,0 +1,22 @@
|
|
1
|
+
const emitter = require("./emitter");
|
2
|
+
|
3
|
+
module.exports = function future(callback, timeout = 0, defData = null) {
|
4
|
+
const listener = emitter();
|
5
|
+
const promise = new Promise((resolve, reject) => {
|
6
|
+
listener.on("resolve", (data) => {
|
7
|
+
resolve(data);
|
8
|
+
});
|
9
|
+
listener.on("reject", (data) => {
|
10
|
+
reject(data);
|
11
|
+
});
|
12
|
+
callback(resolve, reject);
|
13
|
+
if (timeout) setTimeout(() => resolve(defData), timeout);
|
14
|
+
});
|
15
|
+
return {
|
16
|
+
pipe: (callback) => promise.then(callback),
|
17
|
+
pipex: (callback) => promise.finally(callback),
|
18
|
+
catch: (callback) => promise.catch(callback),
|
19
|
+
resolve: (data) => listener.emit("resolve", data),
|
20
|
+
reject: (data) => listener.emit("reject", data),
|
21
|
+
};
|
22
|
+
};
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module.exports.generateRandomID = function generateRandomID(length = 12) {
|
2
|
+
const characters =
|
3
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
4
|
+
const charactersLength = characters.length;
|
5
|
+
let randomID = "";
|
6
|
+
|
7
|
+
for (let i = 0; i < length; i++) {
|
8
|
+
const randomIndex = Math.floor(Math.random() * charactersLength);
|
9
|
+
randomID += characters.charAt(randomIndex);
|
10
|
+
}
|
11
|
+
|
12
|
+
return randomID;
|
13
|
+
};
|
@@ -0,0 +1,57 @@
|
|
1
|
+
const path = require("path");
|
2
|
+
const { getFile, file } = require("../modules/fs");
|
3
|
+
const { importYaml } = require("../modules/yaml");
|
4
|
+
const { findPackage, getPackage } = require("../pkgs/pkgs");
|
5
|
+
|
6
|
+
module.exports.imp = function (runPath, context) {
|
7
|
+
return function (filename, options = {}) {
|
8
|
+
let type = options.type || "coffee";
|
9
|
+
let exports,
|
10
|
+
ispkg = findPackage(filename);
|
11
|
+
|
12
|
+
const filepath = path.resolve(path.dirname(context.module.filepath), filename);
|
13
|
+
|
14
|
+
// console.log(typeof runPath);
|
15
|
+
|
16
|
+
if (ispkg) {
|
17
|
+
exports = getPackage(filename)(context);
|
18
|
+
} else if (type == "coffee") {
|
19
|
+
exports = runPath(
|
20
|
+
filepath,
|
21
|
+
{ ...options, useContext: true },
|
22
|
+
context,
|
23
|
+
).context.module.exports;
|
24
|
+
} else if (type == "js") {
|
25
|
+
exports = runPath(
|
26
|
+
filepath,
|
27
|
+
{ ...options, useContext: true, compile: false },
|
28
|
+
context,
|
29
|
+
).context.module.exports;
|
30
|
+
} else if (type == "yaml" || type == "json" || type == "text") {
|
31
|
+
const f = getFile(filepath);
|
32
|
+
if (type == "yaml") {
|
33
|
+
exports = importYaml(f.path, f);
|
34
|
+
} else if (type == "json") {
|
35
|
+
try {
|
36
|
+
exports = JSON.parse(f.content);
|
37
|
+
} catch (e) {
|
38
|
+
exports = {};
|
39
|
+
}
|
40
|
+
} else {
|
41
|
+
exports = f.content;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
if (options.save && (type == "js" || type == "coffee")) {
|
46
|
+
if (typeof options.save == "string") context[options.save] = exports[i];
|
47
|
+
else
|
48
|
+
for (let i in exports) {
|
49
|
+
context[i] = exports[i];
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
if(!ispkg) context.module.imports.push(filepath);
|
54
|
+
|
55
|
+
return exports;
|
56
|
+
};
|
57
|
+
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
module.exports.map = function map(...args) {
|
5
|
+
if (args.length % 2 !== 0) {
|
6
|
+
throw new Error('Arguments must be in key-value pairs');
|
7
|
+
}
|
8
|
+
|
9
|
+
const result = new Map();
|
10
|
+
for (let i = 0; i < args.length; i += 2) {
|
11
|
+
const key = args[i];
|
12
|
+
const value = args[i + 1];
|
13
|
+
result.set(key, value);
|
14
|
+
}
|
15
|
+
|
16
|
+
return result;
|
17
|
+
};
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
const SerializableData = ["string", "number", "boolean"];
|
3
|
+
|
4
|
+
const isRegExp = (obj) => Object.prototype.toString.call(obj) === '[object RegExp]';
|
5
|
+
|
6
|
+
module.exports.match = function match(value, templates, props) {
|
7
|
+
const matchProps = (pattern, value) => {
|
8
|
+
if (!props) return false;
|
9
|
+
if (typeof props === "object") {
|
10
|
+
let t = true;
|
11
|
+
for (let i in props) {
|
12
|
+
t = t && pattern[i] == value[i];
|
13
|
+
if (!t) break;
|
14
|
+
}
|
15
|
+
return t;
|
16
|
+
} else if (typeof props === "function") {
|
17
|
+
return props(pattern, value);
|
18
|
+
} else {
|
19
|
+
return false;
|
20
|
+
}
|
21
|
+
};
|
22
|
+
|
23
|
+
const matchRegex = (pattern, value) => pattern.test(value);
|
24
|
+
|
25
|
+
const entries = templates instanceof Map ? templates.entries() : Array.isArray(templates) ? templates : Object.entries(templates);
|
26
|
+
|
27
|
+
for (const [pattern, callback] of entries) {
|
28
|
+
if (isRegExp(pattern) ? matchRegex(pattern, value) : SerializableData.includes(typeof value) ? pattern == value : (isRegExp(pattern) ? matchRegex(pattern, value) : (typeof pattern === "function" ? value instanceof pattern || value?.__proto__?.['@instance'] === pattern : matchProps(pattern, value)))) {
|
29
|
+
return callback(...(isRegExp(pattern) ? pattern.exec(value) : [value]));
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
return null;
|
34
|
+
};
|
@@ -0,0 +1,96 @@
|
|
1
|
+
|
2
|
+
const _defaultConstructors = {
|
3
|
+
string: String,
|
4
|
+
array: Array,
|
5
|
+
number: Number,
|
6
|
+
bigint: BigInt,
|
7
|
+
boolean: Boolean,
|
8
|
+
symbol: Symbol,
|
9
|
+
undefined: Object,
|
10
|
+
object: Object,
|
11
|
+
function: Function
|
12
|
+
}
|
13
|
+
|
14
|
+
function getType(value){
|
15
|
+
return typeof value === 'object' ? Array.isArray(value) ? 'array' : typeof value : typeof value;
|
16
|
+
}
|
17
|
+
|
18
|
+
function typedef(value) {
|
19
|
+
return {
|
20
|
+
defaultValue: value,
|
21
|
+
class: typeof value == 'function' ? value : typeof value === 'object' && value !== null && !Array.isArray(value) ? value.constructor : _defaultConstructors[getType(value)],
|
22
|
+
type: getType(value),
|
23
|
+
isConstucted: typeof value === 'object' && value !== null && !Array.isArray(value),
|
24
|
+
isEmpty: typeof value == "object" ? !Object.keys(value).length : typeof value == "string" ? value == "" : true
|
25
|
+
};
|
26
|
+
}
|
27
|
+
|
28
|
+
function typeis(obj, typeDef) {
|
29
|
+
if (typeDef.isConstucted && typeDef.class && !(obj instanceof typeDef.class)) {
|
30
|
+
return false;
|
31
|
+
}
|
32
|
+
|
33
|
+
if(getType(obj) == "object" && typeDef.type == 'function') {
|
34
|
+
return (obj instanceof typeDef.class);
|
35
|
+
}
|
36
|
+
|
37
|
+
if(getType(obj) !== typeDef.type){
|
38
|
+
return false;
|
39
|
+
}
|
40
|
+
|
41
|
+
if(!typeDef.isEmpty) {
|
42
|
+
if(typeDef.type == 'object'){
|
43
|
+
for (const key in typeDef.defaultValue) {
|
44
|
+
const propTypeDef = typeDef.defaultValue[key];
|
45
|
+
|
46
|
+
if (typeof propTypeDef === 'object') {
|
47
|
+
if (!typeis(obj[key], propTypeDef)) {
|
48
|
+
return false;
|
49
|
+
}
|
50
|
+
} else if (typeof obj[key] !== propTypeDef) {
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
}
|
54
|
+
} else if(typeDef.type == 'string'){
|
55
|
+
return typeDef.defaultValue == obj;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
return true;
|
60
|
+
}
|
61
|
+
|
62
|
+
function typex(child, parent) {
|
63
|
+
return child.prototype instanceof parent || child === parent;
|
64
|
+
}
|
65
|
+
|
66
|
+
function typei(child, parent) {
|
67
|
+
return child instanceof parent || child.constructor === parent;
|
68
|
+
}
|
69
|
+
|
70
|
+
module.exports = {
|
71
|
+
typex,
|
72
|
+
typei,
|
73
|
+
typeis,
|
74
|
+
typedef
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
// const f = typedef('');
|
79
|
+
// const fg = typedef({ g: f });
|
80
|
+
|
81
|
+
// const g = { g: 'sss' };
|
82
|
+
|
83
|
+
// class L {}
|
84
|
+
|
85
|
+
// class N {}
|
86
|
+
|
87
|
+
// class M extends N{}
|
88
|
+
|
89
|
+
// let n = new N
|
90
|
+
// let m = new M
|
91
|
+
// let l = new L
|
92
|
+
|
93
|
+
// let tn = typedef(n)
|
94
|
+
|
95
|
+
// // console.log(typeis(g, fg), typeis(1, f), typei('', String));
|
96
|
+
// console.log(typeis(l, tn));
|