Skip to content

Commit

Permalink
Created the Mutable token
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanPB committed Jan 5, 2022
1 parent 9140fe0 commit 8d006f3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/example/mutable-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Events, initSimpleBar, ProgressBar, Tokens} from '@nathanpb/progress'

const messages = ['Stage Bondia', 'Help', 'Foo', 'Bar', 'Cool']
const bar = new ProgressBar({ total: 100 })
const token = Tokens.mutable('Initial')

initSimpleBar({
bar,
template: '[$bar$] $message$',
stream: process.stdout,
tokens: {
bar: Tokens.bar({ length: 30 }),
message: token
},
})

bar.on(Events.COMPLETED, () => console.log('Bar completed'))

setInterval(() => bar.tick(1), 500)
setInterval(() => {
token.setValue(messages[Math.floor(Math.random()*messages.length)])
}, 1500)
3 changes: 3 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
[example/download-lib.ts](/example/download-lib.ts ':include')


# Using the Mutable token
[example/mutable-token.ts](/example/mutable-token.ts ':include')

# Writing a custom token
[example/qcustom-token.ts](/example/custom-token.ts ':include')

Expand Down
1 change: 1 addition & 0 deletions docs/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Tokens.progress()
- [Tokens.title](/api/modules/Tokens.html#title)
- [Tokens.rate](/api/modules/Tokens.html#rate)
- [Tokens.eta](/api/modules/Tokens.html#eta)
- [Tokens.mutable](/api/modules/Tokens.html#mutable)

## Writing a custom token

Expand Down
25 changes: 25 additions & 0 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,28 @@ export const eta = (
{ interval = 1, ...other }: { interval?: number } & NumberFormat = {}
): Token => bar =>
formatNumber(bar.eta() / interval, other)

/**
* The Mutable token stores an internal string, which can be mutated using the `getValue` and `setValue` methods
*
* Example:
* ```
* const token = Tokens.mutable('Foo')
* const bar = new ProgressBar()
*
* console.log(token.getValue()) // Foo
* console.log(token(bar)) // Foo
*
* token.setValue('Bar')
* console.log(token.getValue()) // Bar
* console.log(token(bar)) // Bar
* ```
*
* @param initialValue The starting value for the token
*/
export const mutable = (initialValue: string): (Token & { getValue: ()=>string, setValue: (value: string)=>void }) => {
const func = () => initialValue
func.setValue = (value: string) => { initialValue = value }
func.getValue = () => initialValue
return func
}
25 changes: 25 additions & 0 deletions test/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,29 @@ describe('Tokens.ts', () => {
const bar = new ProgressBar({ title: 'FooBar' })
it('Should be FooBar', () => expect(bar.title).toEqual('FooBar'))
})

describe('#mutable', () => {
it('Should render the initial value of Foo', () => {
const token = Tokens.mutable('Foo')
const bar = new ProgressBar()
expect(token(bar)).toEqual('Foo')
})

describe('#setValue plus #getValue', () => {
it('Should return the initial value of Foo, then mutate the value to Bar and retrieve it', () => {
const token = Tokens.mutable('Foo')
expect(token.getValue()).toEqual('Foo')
token.setValue('Bar')
expect(token.getValue()).toEqual('Bar')
})
})

describe('#setValue plus invoker', () => {
it('Should mutate the Token\'s value from Foo to Bar', () => {
const token = Tokens.mutable('Foo')
token.setValue('Bar')
expect(token(new ProgressBar())).toEqual('Bar')
})
})
});
})

0 comments on commit 8d006f3

Please sign in to comment.