Skip to content

Commit

Permalink
Merge pull request #5 from tguzik/master
Browse files Browse the repository at this point in the history
Export the newestSpace function
  • Loading branch information
JonAbrams authored Nov 18, 2018
2 parents ed39530 + 84907ba commit a37562e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ Parameter: A space
Returns: The root space associated with the given space.
**Note**: Returns the latest root space of the passed in space. This means you can safely do `rootOf(space)` anywhere in a custom action, and always get the latest version.
**Note**: This function goes all the way up in the chain of parent spaces, returning the latest root space of the parameter. This means you can safely do `rootOf(space)` anywhere in a custom action, and always get the latest version.
```js
import { createSpace, rootOf } from 'spaceace';
Expand All @@ -383,6 +383,22 @@ space.user(({ merge, space }, name) => {
})('Sam');
```
### newestSpace
Parameter: A space
Returns: The newest copy of the space, at the same level.
```js
import { createSpace, newestSpace } from 'spaceace';

const space = createSpace({ user: { name: 'Frodo' } });
const latestSnapshot = space.user({ name: 'Sam' });

latestSnapshot.name === 'Sam'; // true
newestSpace(space.user).name === 'Sam'; // true
```
## toJSON
Returns: The contents of a space as an object literal.
Expand Down
1 change: 1 addition & 0 deletions lib/Space.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface Subscriber {
}

export function rootOf(space: Space): Space;
export function newestSpace(space: Space): Space;
export function isSpace(space: Space | any): boolean;
export function subscribe(subscriber: Subscriber): boolean;
export function createSpace(initialState: object): Space;
1 change: 1 addition & 0 deletions lib/Space.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
Space.isSpace = isSpace;
Space.rootOf = rootOf;
Space.createSpace = createSpace;
Space.newestSpace = newestSpace;

/* Instance methods */

Expand Down
30 changes: 29 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
'use strict';

const assert = require('assert');
const { createSpace, subscribe, isSpace, rootOf } = require('../lib/Space');
const {
createSpace,
subscribe,
isSpace,
rootOf,
newestSpace,
} = require('../lib/Space');

describe('Space', function() {
beforeEach(function() {
Expand Down Expand Up @@ -109,13 +115,35 @@ describe('Space', function() {
assert.strictEqual(rootOf(this.space.characters[0]), this.space);
});

it('goes all the way up the chain of parent spaces', function() {
assert.strictEqual(rootOf(this.space.userInfo.location), this.space);
});

it('returns the newest copy of the root space', function() {
this.space.userInfo.location({ city: 'San Diego' });
assert.strictEqual(rootOf(this.space.userInfo.location), this.newSpace);
});

it('throws when given non-space', function() {
assert.throws(() => {
rootOf({});
});
});
});

describe('.newestSpace', function() {
it('returns the same object if no updates were made', function() {
const capture = this.space.userInfo;
assert.strictEqual(newestSpace(capture), capture);
});

it('returns newest copy of current space', function() {
const capture = this.space.userInfo.location;
const newerSpace = capture({ city: 'San Diego' });
assert.strictEqual(newestSpace(capture), newerSpace);
});
});

describe('.toJSON', function() {
it('works', function() {
assert.deepEqual(this.space.toJSON(), this.initialState);
Expand Down

0 comments on commit a37562e

Please sign in to comment.