UNPKG

1.76 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2
3const name = 'ResultSet'
4const dependencies = []
5
6export const createResultSet = /* #__PURE__ */ factory(name, dependencies, () => {
7 /**
8 * A ResultSet contains a list or results
9 * @class ResultSet
10 * @param {Array} entries
11 * @constructor ResultSet
12 */
13 function ResultSet (entries) {
14 if (!(this instanceof ResultSet)) {
15 throw new SyntaxError('Constructor must be called with the new operator')
16 }
17
18 this.entries = entries || []
19 }
20
21 /**
22 * Attach type information
23 */
24 ResultSet.prototype.type = 'ResultSet'
25 ResultSet.prototype.isResultSet = true
26
27 /**
28 * Returns the array with results hold by this ResultSet
29 * @memberof ResultSet
30 * @returns {Array} entries
31 */
32 ResultSet.prototype.valueOf = function () {
33 return this.entries
34 }
35
36 /**
37 * Returns the stringified results of the ResultSet
38 * @memberof ResultSet
39 * @returns {string} string
40 */
41 ResultSet.prototype.toString = function () {
42 return '[' + this.entries.join(', ') + ']'
43 }
44
45 /**
46 * Get a JSON representation of the ResultSet
47 * @memberof ResultSet
48 * @returns {Object} Returns a JSON object structured as:
49 * `{"mathjs": "ResultSet", "entries": [...]}`
50 */
51 ResultSet.prototype.toJSON = function () {
52 return {
53 mathjs: 'ResultSet',
54 entries: this.entries
55 }
56 }
57
58 /**
59 * Instantiate a ResultSet from a JSON object
60 * @memberof ResultSet
61 * @param {Object} json A JSON object structured as:
62 * `{"mathjs": "ResultSet", "entries": [...]}`
63 * @return {ResultSet}
64 */
65 ResultSet.fromJSON = function (json) {
66 return new ResultSet(json.entries)
67 }
68
69 return ResultSet
70}, { isClass: true })