Skip to content

Commit c4f11e2

Browse files
authored
Merge pull request #3128 from jspsych/docs-add-utils-reference
Document the `utils` module and reference it in the randomization docs
2 parents 81612ff + 8834532 commit c4f11e2

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

docs/reference/jspsych-randomization.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ output:
8989
full_design = {
9090
stimulus: ['a.jpg','b.jpg','b.jpg','a.jpg'],
9191
ms_delay: [200, 100, 200, 100]
92-
]
92+
}
9393
*/
9494
```
9595

@@ -182,6 +182,8 @@ This method takes an array of values and generates a new random order of the arr
182182

183183
If the array elements are objects with the same set of properties, then this method can optionally return a single object where each property is a randomized order of the properties defined in the original set of objects. This is useful for randomizing sets of parameters that are used to define a jsPsych block.
184184

185+
This returns a shallow copy of the array, i.e. modifications to arrays/objects within this array will affect the original. If this is not desired, consider taking a [`deepCopy`](./jspsych-utils.md#jspsychutilsdeepcopy).
186+
185187
### Examples
186188

187189
#### Shuffle an array, no repeats
@@ -414,6 +416,8 @@ An array containing the sample.
414416

415417
This method returns a sample drawn at random from a set of values with replacement. The relative probability of drawing each item can be controlled by specifying the `weights`.
416418

419+
This returns a shallow copy of the array, i.e. modifications to arrays/objects within this array will affect the original. If this is not desired, consider taking a [`deepCopy`](./jspsych-utils.md#jspsychutilsdeepcopy).
420+
417421
### Examples
418422

419423
#### Sample with equal probability
@@ -455,6 +459,8 @@ An array containing the sample.
455459

456460
This method returns a sample drawn at random from a set of values without replacement. The sample size must be less than or equal to the length of the array.
457461

462+
This returns a shallow copy of the array, i.e. modifications to arrays/objects within this array will affect the original. If this is not desired, consider taking a [`deepCopy`](./jspsych-utils.md#jspsychutilsdeepcopy).
463+
458464
### Examples
459465

460466
#### Sample without replacement
@@ -532,6 +538,8 @@ Returns an array with the same elements as the input array in a random order.
532538

533539
A simple method for shuffling the order of an array.
534540

541+
This returns a shallow copy of the array, i.e. modifications to arrays/objects within this array will affect the original. If this is not desired, consider taking a [`deepCopy`](./jspsych-utils.md#jspsychutilsdeepcopy).
542+
535543
### Examples
536544

537545
#### Shuffle an array
@@ -565,6 +573,8 @@ Returns an array with the same elements as the input array in a random order, wi
565573

566574
Shuffle an array, ensuring that neighboring elements in the array are different.
567575

576+
This returns a shallow copy of the array, i.e. modifications to arrays/objects within this array will affect the original. If this is not desired, consider taking a [`deepCopy`](./jspsych-utils.md#jspsychutilsdeepcopy).
577+
568578
*Warning: if you provide an array that has very few valid permutations with no neighboring elements, then this method will fail and cause the browser to hang.*
569579

570580
### Examples

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
@@ -71,6 +71,7 @@ nav:
7171
- 'jsPsych.data': 'reference/jspsych-data.md'
7272
- 'jsPsych.randomization': 'reference/jspsych-randomization.md'
7373
- 'jsPsych.turk': 'reference/jspsych-turk.md'
74+
- 'jsPsych.utils': 'reference/jspsych-utils.md'
7475
- 'jsPsych.pluginAPI': 'reference/jspsych-pluginAPI.md'
7576
- Plugins:
7677
- 'List of Plugins': 'plugins/list-of-plugins.md'

0 commit comments

Comments
 (0)