aboutsummaryrefslogtreecommitdiff
path: root/common/entities.js
blob: 3ccbf04e2be5641ec58f758ab1e4bcadd8a00c6a (about) (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 * This file is part of Haketilo.
 *
 * Function: Operations on resources and mappings.
 *
 * Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * As additional permission under GNU GPL version 3 section 7, you
 * may distribute forms of that code without the copy of the GNU
 * GPL normally required by section 4, provided you include this
 * license notice and, in case of non-source distribution, a URL
 * through which recipients can access the Corresponding Source.
 * If you modify file(s) with this exception, you may extend this
 * exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this
 * exception statement from your version.
 *
 * As a special exception to the GPL, any HTML file which merely
 * makes function calls to this code, and for that purpose
 * includes it by reference shall be deemed a separate work for
 * copyright law purposes. If you modify this code, you may extend
 * this exception to your version of the code, but you are not
 * obligated to do so. If you do not wish to do so, delete this
 * exception statement from your version.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
 * license. Although I request that you do not make use of this code in a
 * proprietary program, I am not going to enforce this in court.
 */

/*
 * Convert ver_str into an array representation, e.g. for ver_str="4.6.13.0"
 * return [4, 6, 13, 0].
 */
const parse_version = ver_str => ver_str.split(".").map(n => parseInt(n));

/*
 * ver is an array of integers. rev is an optional integer. Produce string
 * representation of version (optionally with revision number), like:
 *     1.2.3-5
 * No version normalization is performed.
 */
const version_string = (ver, rev=0) => ver.join(".") + (rev ? `-${rev}` : "");
#EXPORT version_string

/*
 * This function overloads on the number of arguments. If one argument is
 * passed, it is an item definition (it need not be complete, only identifier,
 * version and, if applicable, revision properties are relevant). If two or
 * three arguments are given, they are in order: item identifier, item version
 * and item revision.
 * Returned is a string identifying this version of item.
 */
function item_id_string(...args) {
    let def = args[0]
    if (args.length > 1)
	def = {identifier: args[0], version: args[1], revision: args[2]};
    return !Array.isArray(def.version) ? def.identifier :
	`${def.identifier}-${version_string(def.version, def.revision)}`;
}
#EXPORT item_id_string

/* vers should be an array of comparable values. Return the greatest one. */
const max = vals => Array.reduce(vals, (v1, v2) => v1 > v2 ? v1 : v2);

/*
 * versioned_item should be a dict with keys being version strings and values
 * being definitions of the respective versions of a single resource/mapping.
 * Example:
 *     {
 *         "1": {
 *             version: [1]//,
 *             // more stuff
 *         },
 *         "1.1": {
 *             version: [1, 1]//,
 *             // more stuff
 *         }
 *      }
 *
 * Returns the definition with the highest version.
 */
function get_newest_version(versioned_item)
{
    const best_ver = max(Object.keys(versioned_item).map(parse_version));
    return versioned_item[version_string(best_ver)];
}
#EXPORT get_newest_version AS get_newest

/*
 * Returns true if the argument is a nonempty array of numbers without trailing
 * zeros.
 */
function is_valid_version(version) {
    return Array.isArray(version) && version.length > 0 &&
	version.every(n => typeof n === "number") &&
	version[version.length - 1] !== 0;
}
#EXPORT is_valid_version

/*
 * item is a definition of a resource or mapping. Yield all file references
 * (objects with `file` and `sha256` properties) this definition has.
 */
function* get_used_files(item)
{
    for (const file of item.source_copyright)
	yield file;

    if (item.type === "resource") {
	for (const file of item.scripts || [])
	    yield file;
    }
}
#EXPORT get_used_files AS get_files

#IF NEVER

/*
 * Note: the functions below were overeagerly written and are not used now but
 * might prove useful to once we add more functionalities and are hence kept...
 */

/*
 * Clone recursively all objects. Leave other items (arrays, strings) untouched.
 */
function deep_object_copy(object)
{
    const orig = {object};
    const result = {};
    const to_copy = [[orig, {}]];

    while (to_copy.length > 0) {
	const [object, copy] = to_copy.pop();

	for (const [key, value] of Object.entries(object)) {
	    copy[key] = value;

	    if (typeof value === "object" && !Array.isArray(value)) {
		const value_copy = {};
		to_copy.push([value, value_copy]);
		copy[key] = value_copy;
	    }
	}
    }

    return result.orig;
}

/* helper function for normalize_version() */
const version_reductor = (acc, n) => [...(n || acc.length ? [n] : []), ...acc];
/*
 * ver is an array of integers. Strip right-most zeroes from ver.
 *
 * Returns a *new* array. Doesn't modify its argument.
 */
const normalize_version = ver => Array.reduceRight(ver, version_reductor, []);

#ENDIF