Skip to content

Commit 36e64ef

Browse files
committed
Add docs page for the utils module
1 parent 6d8aca9 commit 36e64ef

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

docs/reference/jspsych-utils.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ nav:
7070
- 'jsPsych.data': 'reference/jspsych-data.md'
7171
- 'jsPsych.randomization': 'reference/jspsych-randomization.md'
7272
- 'jsPsych.turk': 'reference/jspsych-turk.md'
73+
- 'jsPsych.utils': 'reference/jspsych-utils.md'
7374
- 'jsPsych.pluginAPI': 'reference/jspsych-pluginAPI.md'
7475
- Plugins:
7576
- 'List of Plugins': 'plugins/list-of-plugins.md'

0 commit comments

Comments
 (0)