-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
132 lines (114 loc) · 3.34 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Martin Pravda
// utils.js
// A simple utility functions
/*jslint browser, node, bitwise */
function make_empty_list(length) {
return [...new Array(length)];
}
//demo make_empty_list(5);
function decode_text(byte_array, encoding = "utf-8") {
return new TextDecoder(encoding).decode(byte_array);
}
//demo const byte_array = new TextEncoder('utf-8').encode("geopackage");
//demo decode_text(byte_array);
function get_int(count) {
return function (view, offset) {
return make_empty_list(count).map(function (ignore, index) {
return view.getInt8(offset + index);
}).reduce(function ([result, shift], value) {
result |= value << shift;
shift -= 8;
return [result, shift];
}, [0, (count * 8) - 8]);
};
}
function pair(first_arg, second_arg) {
return [first_arg, second_arg];
}
//demo pair(1, 2);
function from_pairs(pairs) {
return pairs.reduce(function (record, [key, value]) {
record[key] = value;
return record;
}, {});
}
//demo from_pairs(["a", 1], ["b", 2]);
function zip(first = [], second = []) {
let list = make_empty_list(first.length);
if (first.length !== second.length) {
list = make_empty_list(
first.length < second.length
? first.length
: second.length
);
}
return list.map(function (ignore, index) {
return pair(first[index], second[index]);
});
}
//demo zip([1,2], [3,4]);
/**
* Parses an SQL CREATE TABLE statement and returns an array of column definitions.
*
* @param {string} sql - The SQL statement to parse.
* @returns {Array<{name: string, type: string}>} - An array of column definitions, each with a `name` and `type` property.
*/
function from_sql(sql = "") {
const match = sql.match(/\((.*)\)/);
if (!match) {
return [];
}
return match[1].split(",").map(function (col) {
const parts = col.trim().split(" ");
return {
name: parts[0],
type: parts[1]
};
});
}
// demo from_sql("CREATE TABLE (id integer, name text, age integer)");
//test import jscheck from "./tools/jscheck.js";
//test const jsc = jscheck();
//test jsc.claim(
//test "test zip for the same size of both arrays",
//test function predicate(verdict, a, b) {
//test return verdict(zip(a, b).length === 2);
//test },
//test [
//test jsc.array(2, jsc.integer(1000)),
//test jsc.array(2, jsc.integer(1000))
//test ]
//test );
//test jsc.claim(
//test "test zip for a shorter size of the first array",
//test function predicate(verdict, a, b) {
//test return verdict(zip(a, b).length === 2);
//test },
//test [
//test jsc.array(4, jsc.integer(1000)),
//test jsc.array(2, jsc.integer(1000))
//test ]
//test );
//test jsc.claim(
//test "test zip for a shorter size of the second array",
//test function predicate(verdict, a, b) {
//test return verdict(zip(a, b).length === 2);
//test },
//test [
//test jsc.array(2, jsc.integer(1000)),
//test jsc.array(4, jsc.integer(1000))
//test ]
//test );
//test jsc.check({
//test detail: 4,
//test on_report: console.log
//test });
export default Object.freeze({
decode_text,
from_pairs,
from_sql,
get_int,
make_empty_list,
pair,
zip
});