File: jsonArrayProperty.qml

package info (click to toggle)
qt6-declarative 6.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 308,920 kB
  • sloc: cpp: 775,911; javascript: 514,247; xml: 10,855; python: 2,806; ansic: 2,253; java: 810; sh: 262; makefile: 41; php: 27
file content (191 lines) | stat: -rw-r--r-- 6,482 bytes parent folder | download | duplicates (2)
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
import QtQml
import TypeWithQJsonArrayProperty

TypeWithQJsonArrayProperty {
    function jsArray() { return [1, 2, 3] }

    jsonArray: jsArray()

    property list<int> concatenatedJsonArray: jsonArray.concat([4, 5, 6])
    property list<int> concatenatedJsArray: jsArray().concat([4, 5, 6])

    property bool entriesMatch: {
        var iterator = jsonArray.entries();
        for (var [index, element] of jsArray().entries()) {
            var v = iterator.next().value;
            if (index !== v[0] || element !== v[1]) {
                console.log(index, v[0], element, v[1]);
                return false;
            }
        }

        var iterator = jsArray().entries();
        for (var [index, element] of jsonArray.entries()) {
            var v = iterator.next().value;
            if (index !== v[0] || element !== v[1]) {
                console.log(index, v[0], element, v[1]);
                return false;
            }
        }

        return true;
    }

    property bool jsonArrayEvery: jsonArray.every(element => element != 0)
    property bool jsArrayEvery: jsArray().every(element => element != 0)

    property list<int> jsonArrayFiltered: jsonArray.filter(element => element > 2)
    property list<int> jsArrayFiltered: jsArray().filter(element => element > 2)

    property int jsonArrayFind: jsonArray.find(element => element === 2)
    property int jsArrayFind: jsArray().find(element => element === 2)

    property int jsonArrayFindIndex: jsonArray.findIndex(element => element === 1)
    property int jsArrayFindIndex: jsArray().findIndex(element => element === 1)

    property string jsonArrayForEach
    property string jsArrayForEach

    property bool jsonArrayIncludes: jsonArray.includes(3)
    property bool jsArrayIncludes: jsArray().includes(3)

    property int jsonArrayIndexOf: jsonArray.indexOf(2)
    property int jsArrayIndexOf: jsArray().indexOf(2)

    property string jsonArrayJoin: jsonArray.join()
    property string jsArrayJoin: jsArray().join()

    property bool keysMatch: {
        var iterator = jsonArray.keys();
        for (var index of jsArray().keys()) {
            var v = iterator.next().value;
            if (index !== v) {
                console.log(index, v);
                return false;
            }
        }

        var iterator = jsArray().keys();
        for (var index of jsonArray.keys()) {
            var v = iterator.next().value;
            if (index !== v) {
                console.log(index, v);
                return false;
            }
        }

        return true;
    }

    property int jsonArrayLastIndexOf: jsonArray.lastIndexOf(1)
    property int jsArrayLastIndexOf: jsArray().lastIndexOf(1)

    property list<string> jsonArrayMap: jsonArray.map(element => element.toString())
    property list<string> jsArrayMap: jsArray().map(element => element.toString())

    property int jsonArrayReduce: jsonArray.reduce((acc, element) => acc - element, 40)
    property int jsArrayReduce: jsArray().reduce((acc, element) => acc - element, 40)

    property string jsonArrayReduceRight: jsonArray.reduceRight((acc, element) => acc + element.toString(), "")
    property string jsArrayReduceRight: jsArray().reduceRight((acc, element) => acc + element.toString(), "")

    property list<int> jsonArraySlice: jsonArray.slice(0, 1)
    property list<int> jsArraySlice: jsArray().slice(0, 1)

    property bool jsonArraySome: jsonArray.some(element => element === 1)
    property bool jsArraySome: jsArray().some(element => element === 1)

    property string stringifiedLocaleJsonArray: jsonArray.toLocaleString()
    property string stringifiedLocaleJsArray: jsArray().toLocaleString()

    property string stringifiedJsonArray: jsonArray.toString()
    property string stringifiedJsArray: jsArray().toString()

    property bool valuesMatch: {
        var iterator = jsonArray.values();
        for (var obj of jsArray().values()) {
            var v = iterator.next().value;
            if (obj !== v) {
                console.log(obj, v);
                return false;
            }
        }

        var iterator = jsArray().values();
        for (var obj of jsonArray.values()) {
            var v = iterator.next().value;
            if (obj !== v) {
                console.log(obj, v);
                return false;
            }
        }

        return true;
    }

    // In-place mutation methods.
    // Set by onCompleted if mutating jsonArray and then accessing it
    // respects the mutation and the mutation behaves as for an array.
    property bool jsonArrayWasCopiedWithin: false
    property bool jsonArrayWasFilled: false
    property bool jsonArrayWasPopped: false
    property bool jsonArrayWasPushed: false
    property bool jsonArrayWasReversed: false
    property bool jsonArrayWasShifted: false
    property bool jsonArrayWasSpliced: false
    property bool jsonArrayWasUnshifted: false
    property bool jsonArrayWasSorted: false

    Component.onCompleted: {
        function equals(lhs, rhs) {
            return lhs.toString() === rhs.toString()
        }

        jsonArray.forEach(element =>  jsonArrayForEach += "-" + element + "-");
        jsArray().forEach(element =>  jsArrayForEach += "-" + element + "-");

        var array = jsArray()

        jsonArray.copyWithin(1, 0, 1)
        array.copyWithin(1, 0, 1)
        jsonArrayWasCopiedWithin = equals(jsonArray, array)

        jsonArray.fill(7, 0, 1)
        array.fill(7, 0, 1)
        jsonArrayWasFilled = equals(jsonArray, array)

        jsonArray.pop()
        array.pop()
        jsonArrayWasPopped = equals(jsonArray, array)

        jsonArray.push(23)
        jsonArray.push(11)
        jsonArray.push(54)
        jsonArray.push(42)
        array.push(23)
        array.push(11)
        array.push(54)
        array.push(42)
        jsonArrayWasPushed = equals(jsonArray, array)

        jsonArray.reverse()
        array.reverse()
        jsonArrayWasReversed = equals(jsonArray, array)

        jsonArray.shift()
        array.shift()
        jsonArrayWasShifted = equals(jsonArray, array)

        jsonArray.splice(2, 1, [1, 2], 7, [1, 5])
        array.splice(2, 1, [1, 2], 7, [1, 5])
        jsonArrayWasSpliced = equals(jsonArray, array)

        jsonArray.unshift(4, 71)
        array.unshift(4, 71)
        jsonArrayWasUnshifted = equals(jsonArray, array)

        jsonArray.sort()
        array.sort()
        jsonArrayWasSorted = equals(jsonArray, array)
    }
}