Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ryanve/cueing
JavaScript utility to cue, seek, or cycle items
https://github.com/ryanve/cueing
arrays cycle cycler javascript module playlists
Last synced: about 1 month ago
JSON representation
JavaScript utility to cue, seek, or cycle items
- Host: GitHub
- URL: https://github.com/ryanve/cueing
- Owner: ryanve
- License: mit
- Created: 2014-05-01T19:48:00.000Z (over 10 years ago)
- Default Branch: gh-pages
- Last Pushed: 2017-03-12T01:28:14.000Z (over 7 years ago)
- Last Synced: 2024-08-08T17:43:30.310Z (3 months ago)
- Topics: arrays, cycle, cycler, javascript, module, playlists
- Language: JavaScript
- Homepage: http://ryanve.dev/cueing/
- Size: 29.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cueing ►►|
Cue, seek, or cycle items in JavaScript in node or the browser### [npm](https://www.npmjs.com/package/cueing)
```
npm install cueing --save
```### Basic usage
```js
var cueing = require('cueing')
var letters = cueing(['a', 'b', 'c'])// cue the last index and then read from there
letters.cue(-1) // => letters
letters.seek(0) // => 'c'
letters.seek(1) // => 'a'// manually move the needle
letters.needle(0)// seek backwards
letters.seek(-1) // => 'c'// seeking (or cueing) will loop around
letters.seek(-4) // => 'b'
```## API
### `cueing(pool=[])`
- Get a new `cueing` instance
- `pool`: `number|{length:number}` length or array(-like) of values
- → `cueing` instance#### `cueing()` instances are array-like and inherit array methods
```js
var tracks = cueing(['1.mp3', '2.mp3', '3.mp3'])
tracks.join() // '1.mp3,2.mp3,3.mp3'
tracks instanceof cueing // true
tracks instanceof Array // true
Array.isArray(tracks) // falsecueing(3).every(function(v, i, range) {
// Cueing objects are made dense for use with array iterators
return undefined === v && i in range && range instanceof cueing
}) // true
```#### `cueing()` objects coerce to their needle position
```js
var cueing = require('cueing')
var list = cueing(['a', 'b', 'c'])
+list // 0
isFinite(list) // true
list.needle(2) // move the needle to a new index
2 == list // true
+list // 2
String(list) // '2'
list[list] // 'c'
```### `cueing()` methods
#### `.needle(index?)`
- Manually move the needle to `index`
- `index`: `number|cueing` destination index
- → `this`#### `.cue(offset=0)`
- Move the needle by `offset`
- `offset`: `number|cueing` +/- integer to move the needle by
- → `this`#### `.seek(offset=0)`
- `.cue` the offset and get the value there
- `offset`: `number|cueing` +/- integer to move the needle by
- → `*`#### `.store()`
- Store the current needle position for recalling later
- → `this`#### `.recall(index?)`
- Recall stored cue point(s)
- `index`: `number|cueing` +/- index to read. `0` reads the oldest point. `-1` reads the newest.
- → `cueing` instance at recalled state#### `.clear()`
- Clear stored cue points
- → `this`#### `.clone()`
- Clone (copy) a `cueing` instance at its current state to a new instance
- → `cueing` clone### Static methods
#### `cueing.cue(pool, start=0, offset=0)`
- Get the `pool` index that is `offset` away from `start`
- → `number````js
cueing.cue(['a', 'b', 'c'], -1) // => 2
cueing.cue(['a', 'b', 'c'], 1, -2) // => 2
```#### `cueing.seek(pool, start=0, offset=0)`
- Get the `pool` value that is `offset` away from `start`
- → `*````js
cueing.seek(['a', 'b', 'c'], 1, -1) // => 'a'
cueing.seek(['a', 'b', 'c'], 0, 5) // => 'b'
```#### `cueing.store(array, point)`
- Push `point` onto `array` if different from last point
- → `array` reference```js
cueing.store([1, 5], 3) // => [1, 5, 3]
cueing.store([1, 5], 5) // => [1, 5]
cueing.store([1, 5], 1) // => [1, 5, 1]
```## Playground
[Try `cueing` in the browser](http://ryanve.github.io/cueing/)