Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Add startsWith method to SchemaContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Jan 3, 2020
1 parent fd8c9f6 commit b596ebc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,23 @@ export class SchemaContext {
endsWith( query ) {
return Array.from( this.getNames() ).join( ' ' ).endsWith( query );
}

/**
* Checks whether the context starts with the given nodes.
*
* const ctx = new SchemaContext( [ rootElement, paragraphElement, textNode ] );
*
* ctx.endsWith( '$root' ); // -> true
* ctx.endsWith( '$root paragraph' ); // -> true
* ctx.endsWith( '$text' ); // -> false
* ctx.endsWith( 'paragraph' ); // -> false
*
* @param {String} query
* @returns {Boolean}
*/
startsWith( query ) {
return Array.from( this.getNames() ).join( ' ' ).startsWith( query );
}
}

/**
Expand Down
56 changes: 56 additions & 0 deletions tests/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3249,4 +3249,60 @@ describe( 'SchemaContext', () => {
expect( ctx.endsWith( 'bar', 'foo' ) ).to.be.false;
} );
} );

describe( 'startsWith()', () => {
it( 'returns true if the start of the context matches the query - 1 item', () => {
const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );

expect( ctx.startsWith( 'foo' ) ).to.be.true;
} );

it( 'returns true if the start of the context matches the query - 2 items', () => {
const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );

expect( ctx.startsWith( 'foo bar' ) ).to.be.true;
} );

it( 'returns true if the start of the context matches the query - full match of 3 items', () => {
const ctx = new SchemaContext( [ 'foo', 'bar', 'bom' ] );

expect( ctx.startsWith( 'foo bar bom' ) ).to.be.true;
} );

it( 'returns true if the start of the context matches the query - full match of 1 items', () => {
const ctx = new SchemaContext( [ 'foo' ] );

expect( ctx.startsWith( 'foo' ) ).to.be.true;
} );

it( 'returns true if not only the start of the context matches the query', () => {
const ctx = new SchemaContext( [ 'foo', 'foo', 'foo', 'foo' ] );

expect( ctx.startsWith( 'foo foo' ) ).to.be.true;
} );

it( 'returns false if query matches the middle of the context', () => {
const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );

expect( ctx.startsWith( 'bom' ) ).to.be.false;
} );

it( 'returns false if query matches the end of the context', () => {
const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );

expect( ctx.startsWith( 'dom' ) ).to.be.false;
} );

it( 'returns false if query does not match', () => {
const ctx = new SchemaContext( [ 'foo', 'bar', 'bom', 'dom' ] );

expect( ctx.startsWith( 'dom bar' ) ).to.be.false;
} );

it( 'returns false if query is longer than context', () => {
const ctx = new SchemaContext( [ 'foo' ] );

expect( ctx.startsWith( 'bar', 'foo' ) ).to.be.false;
} );
} );
} );

0 comments on commit b596ebc

Please sign in to comment.