summaryrefslogtreecommitdiff
path: root/wikipedia/utils.js
blob: 662b2b432873d293a739e14cc411705c98c2a508 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const Gio = imports.gi.Gio;
const GdkPixbuf = imports.gi.GdkPixbuf;

const set_default = function(obj, key, val) {
    // Similar to python's dict.setdefault(key, val)
    if(obj[key] === undefined)
        obj[key] = val;
}

const default_param = function(variable, defaultValue) {
    // Emulate python's default parameter values
    if(variable === undefined)
        return defaultValue;
    else
        return variable;
}

const range = function(a, b, step) {
    // Emulate python's range function
    var A = [];
    if(typeof a == 'number'){
        if(typeof b == 'undefined'){
            b = a - 1;
            a = 0;
        }
        A[0] = a;
        step = step || 1;
        while(a + step < b){
            A[A.length] = a += step;
        }
    }
    return A;
}

/*
 * ...Taken from utils.js from the eos-weather branch...
 */
function load_file_from_resource(filename) {
    // Return the text stored in the file at filename
    var file = Gio.file_new_for_uri(filename);
    var fstream = file.read(null);
    var dstream = new Gio.DataInputStream({
        base_stream: fstream
    });
    var data = dstream.read_until("", null);
    fstream.close(null);
    return data[0];
}

function get_uri_for_relative_path(relative_path){
    let file = Gio.file_new_for_path(relative_path);
    return file.get_uri();
}

function write_contents_to_file(filename, content){
    let file = Gio.file_new_for_path(filename);
    file.replace_contents(content, null, false, 0, null);
}

/*
 * ...Taken from utils.js from the eos-weather branch...
 */
function load_file(filename) {
    // Return the text stored in the file at filename
    var file = Gio.file_new_for_path(filename);
    var fstream = file.read(null);
    var dstream = new Gio.DataInputStream({
        base_stream: fstream
    });
    var data = dstream.read_until("", null);
    fstream.close(null);
    return data[0];
}

/*
 * Compares two lists *a* and *b* to see if they contain
 * the same contents, ignoring order.  Contents are defined
 * to be equal via JSON representation, unless they are
 * primitives, then String() casting is used.
 *
 * If passed non-list objects, this method has undefined
 * behavior. Algorithm is not exceptionally optimized and has
 * O(n^2) performance.
 *
 * Method treats elements that are logically equivalent but
 * not type-equivalent to be the same.  Thus the string of
 * "3" is considered to equal the integer 3 in these comparisons.
 *
 * Returns true if a and b contain the same elements in
 * any order.
 */
const set_equals = function (a, b) {
    if (typeof a != "object" || typeof b != "object") {
        return false;
    }
    if (a.length != b.length) {
        // Optimization
        return false;
    }
    let matched_of_a = 0;
    for (let i = 0; i < a.length; i++) {
        let i_string = typeof a[i] == "object" ? JSON.stringify (a[i]) : String(a[i]);
        for (let j = 0; j < b.length; j++) {
            let j_string = typeof b[j] == "object" ? JSON.stringify (b[j]) : String(b[j]);
            if (j_string == i_string) {
                matched_of_a++;
            }
        }
    }
    let matched_of_b = 0;
    for (let i = 0; i < b.length; i++) {
        let i_string = typeof b[i] == "object" ? JSON.stringify (b[i]) : String(b[i]);
        for (let j = 0; j < a.length; j++) {
            let j_string = typeof a[j] == "object" ? JSON.stringify (a[j]) : String(a[j]);
            if (j_string == i_string) {
                matched_of_b++;
            }
        }
    }
    if (matched_of_a == a.length &&
        matched_of_b == matched_of_a)
    {
        return true;
    }
    return false;
};

/*
 * Checks to see if an array *arr* contains on element *obj*.  If
 * *same_type* is provided (and is true), the objects must
 * actually be the same type as well to be considered part of the
 * array.
 *
 * Somehow reminds the user of a pirate.
 */
const array_contains = function (arr, obj, same_type) {
    if (same_type == true) {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] === obj) {
              return true;
            }
        }
  } else {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] == obj) {
                return true;
            }
        }
  }
  return false;
};

// Convenience function to parse a clutter color from a string
function parse_clutter_color(color_string) {
    let [res, color] = Clutter.Color.from_string(color_string);
    return color;
}

// Convenience function to load a gresource image into a Clutter.Image
function load_clutter_image_from_resource(resource_path) {
    let pixbuf = GdkPixbuf.Pixbuf.new_from_resource(resource_path);
    let image = new Clutter.Image();
    if (pixbuf != null) {
        image.set_data(pixbuf.get_pixels(),
                       pixbuf.get_has_alpha()
                           ? Cogl.PixelFormat.RGBA_8888
                           : Cogl.PixelFormat.RGB_888,
                       pixbuf.get_width(),
                       pixbuf.get_height(),
                       pixbuf.get_rowstride());
    }
    return image;
}

// Private function to format a clutter actors allocation and print it
function _clutter_allocation_printer(actor, box, flag) {
    print("Allocation for", actor);
    print(" Xs:", box.x1, box.x2);
    print(" Ys:", box.y1, box.y2);
}

// Call this function on a clutter actor to have it log it's allocation to
// console
function print_clutter_actor_allocation(actor) {
    actor.connect('allocation-changed', _clutter_allocation_printer);
}

// Convenience function to convert a resource URI to a resource path, for
// APIs that expect a path rather than an URI
function resourceUriToPath(uri) {
    if(uri.startsWith('resource://'))
        return uri.slice('resource://'.length);
    throw new Error('Resource URI did not start with "resource://"');
}