Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Added more state functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronelliott committed Apr 2, 2020
1 parent 60614a9 commit da007ad
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class State extends EventEmitter {
// Array methods
// ---------------------------------------------------------------------------

concat(key, values) {
const new_values = this.getArray(key).concat(values);
this.set(key, new_values);
}

getArray(key) {
const value = this.get(key, []);

Expand Down Expand Up @@ -113,6 +118,12 @@ class State extends EventEmitter {
return value;
}

splice(key, start, deleteCount, ...items) {
const array = this.getArray(key);
array.splice(start, deleteCount, ...items);
this.set(key, array);
}

unshift(key, value) {
this.getArray(key).unshift(value);
this.notifyUpdated(key, this.getArray(key));
Expand Down
2 changes: 2 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ declare module 'kortex' {
// ---------------------------------------------------------------------------
// Array methods
// ---------------------------------------------------------------------------
concat(key: string, values: any[]): void;
getArray(key: string): any[];
filter(key: string, new_key: string, callback: Callback): void;
map(key: string, new_key: string, callback: Callback): void;
reduce(key: string, new_key: string, callback: Callback, starting_value: any): void;
pop(key: string): any;
push(key: string, value: any): void;
shift(key: string): any;
splice(key: string, start: number, deleteCount: number, ...items: any[]): void;
unshift(key: string, value: any): void;

// ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kortex",
"version": "3.2.0",
"version": "3.4.0",
"description": "Dead simple state management for react.",
"main": "lib",
"author": "Ron Elliott <ronaldbelliott@gmail.com>",
Expand Down
48 changes: 48 additions & 0 deletions test/State.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ describe('State', function() {
});

describe('Array', function() {
describe('concat', function () {
it('should throw an error if the given key is not an array', function () {
should(() => {
this.state.concat('nope', ['nope', 'yup', 'totes'])
}).throw('Key "nope" is not an array');
});

it('should push values into the given key', function () {
this.state.store.maybe.should.eql(['dunno', 'could be']);
this.state.concat('maybe', ['nope', 'yup', 'totes']);
this.state.store.maybe.should.eql(['dunno', 'could be', 'nope', 'yup', 'totes']);
});

it('should notify watchers of the key update', function () {
const spy = sinon.spy();
this.state.watch('maybe', spy);
this.state.concat('maybe', ['nope', 'yup', 'totes']);
spy.called.should.equal(true);
});
});

describe('getArray', function() {
it('should throw an error if the given key is not an array', function() {
should(() => {
Expand Down Expand Up @@ -309,6 +330,33 @@ describe('State', function() {
});
});

describe('splice', function () {
it('should throw an error if the given key is not an array', function () {
should(() => {
this.state.splice('nope', 1, 1)
}).throw('Key "nope" is not an array');
});

it('should remove items from the array', function () {
this.state.store.maybe.should.eql(['dunno', 'could be']);
this.state.splice('maybe', 1, 1);
this.state.store.maybe.should.eql(['dunno']);
});

it('should add items to the array', function () {
this.state.store.maybe.should.eql(['dunno', 'could be']);
this.state.splice('maybe', 1, 1, 'hah', 'again');
this.state.store.maybe.should.eql(['dunno', 'hah', 'again']);
});

it('should notify watchers of the key update', function () {
const spy = sinon.spy();
this.state.watch('maybe', spy);
this.state.splice('maybe', 1, 1);
spy.called.should.equal(true);
});
});

describe('unshift', function() {
it('should throw an error if the given key is not an array', function() {
should(() => {
Expand Down

0 comments on commit da007ad

Please sign in to comment.