Reads the keys of a map-like object and returns a new map with the original keys randomly shuffled.
$ ori greetings.yaml
Alice: Hello, Alice.
Bob: Hello, Bob.
Carol: Hello, Carol.
$ ori Tree.shuffle greetings.yaml
Alice: Hello, Alice.
Carol: Hello, Carol.
Bob: Hello, Bob.
$ ori Tree.shuffle greetings.yaml
Bob: Hello, Bob.
Carol: Hello, Carol.
Alice: Hello, Alice.
Producing a random but stable shuffle #
You can call Tree.shuffle with an optional options argument that has a randoms property. This property should be a function that returns either:
- Floating-point numbers between 0 and 1 (like
Math.randomdoes) - Integers (like
Origami.randomsFromdoes)
As discussed in Origami.randomFrom, it can be desirable to incorporate some randomness into your site’s output for variety, while doing so in a way that’s always the same for a given state of your site.
You can apply this idea to a shuffle by telling Tree.shuffle to use the sequence of random numbers produced by Origami.randomsFrom. As long as the input data to randomsFrom in consistent, the shuffled output will be stable.
$ ori Tree.shuffle greetings.yaml, { randoms: Origami.randomsFrom/hello }
Alice: Hello, Alice.
Carol: Hello, Carol.
Bob: Hello, Bob.
$ ori Tree.shuffle greetings.yaml, { randoms: Origami.randomsFrom/hello }
Alice: Hello, Alice.
Carol: Hello, Carol.
Bob: Hello, Bob.