|
| 1 | +# jsPsych.utils |
| 2 | + |
| 3 | +The jsPsych.utils module contains utility functions that might turn out useful at one place or the other. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## jsPsych.utils.unique |
| 8 | + |
| 9 | +```javascript |
| 10 | +jsPsych.utils.unique(array) |
| 11 | +``` |
| 12 | + |
| 13 | +### Parameters |
| 14 | + |
| 15 | +| Parameter | Type | Description | |
| 16 | +| --------- | ----- | ------------------------------ | |
| 17 | +| array | Array | An array of arbitrary elements | |
| 18 | + |
| 19 | +### Return value |
| 20 | + |
| 21 | +An array containing all elements from the input array, but without duplicate elements |
| 22 | + |
| 23 | +### Description |
| 24 | + |
| 25 | +This function takes an array and returns a copy of that array with all duplicate elements removed. |
| 26 | + |
| 27 | +### Example |
| 28 | + |
| 29 | +```javascript |
| 30 | +jsPsych.utils.unique(["a", "b", "b", 1, 1, 2]) // returns ["a", "b", 1, 2] |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## jsPsych.utils.deepCopy |
| 36 | + |
| 37 | +```javascript |
| 38 | +jsPsych.utils.deepCopy(object); |
| 39 | +``` |
| 40 | + |
| 41 | +### Parameters |
| 42 | + |
| 43 | +| Parameter | Type | Description | |
| 44 | +| --------- | --------------- | ---------------------------- | |
| 45 | +| object | Object or Array | An arbitrary object or array | |
| 46 | + |
| 47 | +### Return value |
| 48 | + |
| 49 | +A deep copy of the provided object or array |
| 50 | + |
| 51 | +### Description |
| 52 | + |
| 53 | +This function takes an arbitrary object or array and returns a deep copy of it, i.e. all child objects or arrays are recursively copied too. |
| 54 | + |
| 55 | +### Example |
| 56 | + |
| 57 | +```javascript |
| 58 | +var myObject = { nested: ["array", "of", "elements"] }; |
| 59 | +var deepCopy = jsPsych.utils.deepCopy(myObject); |
| 60 | +deepCopy.nested[2] = "thingies"; |
| 61 | +console.log(myObject.nested[2]) // still logs "elements" |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## jsPsych.utils.deepMerge |
| 67 | + |
| 68 | +```javascript |
| 69 | +jsPsych.utils.deepMerge(object1, object2); |
| 70 | +``` |
| 71 | + |
| 72 | +### Parameters |
| 73 | + |
| 74 | +| Parameter | Type | Description | |
| 75 | +| --------- | ------ | --------------- | |
| 76 | +| object1 | Object | Object to merge | |
| 77 | +| object2 | Object | Object to merge | |
| 78 | + |
| 79 | +### Return value |
| 80 | + |
| 81 | +A deep copy of `object1` with all the (nested) properties from `object2` filled in |
| 82 | + |
| 83 | +### Description |
| 84 | + |
| 85 | +This function takes two objects and recursively merges them into a new object. If both objects define the same (nested) property, the property value from `object2` takes precedence. |
| 86 | + |
| 87 | +### Example |
| 88 | + |
| 89 | +```javascript |
| 90 | +var object1 = { a: 1, b: { c: 1, d: 2 } }; |
| 91 | +var object2 = { b: { c: 2 }, e: 3 }; |
| 92 | +jsPsych.utils.deepMerge(object1, object2); // returns { a: 1, b: { c: 2, d: 2 }, e: 3 } |
| 93 | +``` |
0 commit comments