@scrabble-solver/types 2.8.6 → 2.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/Board.d.ts CHANGED
@@ -4,9 +4,9 @@ declare class Board {
4
4
  static create(width: number, height: number): Board;
5
5
  static fromJson(json: BoardJson): Board;
6
6
  static fromStringArray(stringArray: string[]): Board;
7
+ readonly columnsCount: number;
7
8
  readonly rows: Cell[][];
8
- readonly numberOfColumns: number;
9
- readonly numberOfRows: number;
9
+ readonly rowsCount: number;
10
10
  constructor({ rows }: {
11
11
  rows: Cell[][];
12
12
  });
package/build/Board.js CHANGED
@@ -9,8 +9,8 @@ const Tile_1 = __importDefault(require("./Tile"));
9
9
  class Board {
10
10
  constructor({ rows }) {
11
11
  this.rows = rows;
12
- this.numberOfColumns = rows[0].length;
13
- this.numberOfRows = rows.length;
12
+ this.columnsCount = rows[0].length;
13
+ this.rowsCount = rows.length;
14
14
  }
15
15
  static create(width, height) {
16
16
  return Board.fromStringArray(Array(height).fill(Array(width).fill(' ').join('')));
@@ -31,8 +31,8 @@ class Board {
31
31
  });
32
32
  }
33
33
  get center() {
34
- const x = Math.floor(this.numberOfColumns / 2);
35
- const y = Math.floor(this.numberOfRows / 2);
34
+ const x = Math.floor(this.columnsCount / 2);
35
+ const y = Math.floor(this.rowsCount / 2);
36
36
  return this.rows[y][x];
37
37
  }
38
38
  clone() {
@@ -43,20 +43,20 @@ class Board {
43
43
  return this.collidesUp(cell) || this.collidesDown(cell) || this.collidesLeft(cell) || this.collidesRight(cell);
44
44
  }
45
45
  collidesDown({ x, y }) {
46
- return y < this.numberOfRows - 1 && !this.rows[y + 1][x].isEmpty;
46
+ return y < this.rowsCount - 1 && !this.rows[y + 1][x].isEmpty;
47
47
  }
48
48
  collidesLeft({ x, y }) {
49
49
  return x > 0 && !this.rows[y][x - 1].isEmpty;
50
50
  }
51
51
  collidesRight({ x, y }) {
52
- return x < this.numberOfColumns - 1 && !this.rows[y][x + 1].isEmpty;
52
+ return x < this.columnsCount - 1 && !this.rows[y][x + 1].isEmpty;
53
53
  }
54
54
  collidesUp({ x, y }) {
55
55
  return y > 0 && !this.rows[y - 1][x].isEmpty;
56
56
  }
57
57
  equals(other) {
58
- return (this.numberOfColumns === other.numberOfColumns &&
59
- this.numberOfRows === other.numberOfRows &&
58
+ return (this.columnsCount === other.columnsCount &&
59
+ this.rowsCount === other.rowsCount &&
60
60
  this.rows.every((row, rowIndex) => {
61
61
  return row.every((cell, cellIndex) => {
62
62
  return cell.equals(other.rows[rowIndex][cellIndex]);
@@ -1,3 +1,4 @@
1
1
  import CellJson from './CellJson';
2
- declare type BoardJson = (CellJson | null)[][];
2
+ declare type BoardJson = CellJson[][];
3
+ export declare const isBoardJson: (value: unknown) => value is BoardJson;
3
4
  export default BoardJson;
@@ -1,2 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBoardJson = void 0;
4
+ const CellJson_1 = require("./CellJson");
5
+ const isBoardJson = (value) => {
6
+ if (!Array.isArray(value)) {
7
+ return false;
8
+ }
9
+ for (const row of value) {
10
+ if (!Array.isArray(value)) {
11
+ return false;
12
+ }
13
+ for (const cell of row) {
14
+ if (!(0, CellJson_1.isCellJson)(cell)) {
15
+ return false;
16
+ }
17
+ }
18
+ }
19
+ return true;
20
+ };
21
+ exports.isBoardJson = isBoardJson;
package/build/Cell.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import CellJson from './CellJson';
2
2
  import Tile from './Tile';
3
3
  declare class Cell {
4
- static fromJson(json: CellJson | null): Cell;
5
- static readonly Null: Cell;
4
+ static fromJson(json: CellJson): Cell;
6
5
  readonly isEmpty: boolean;
7
6
  tile: Tile;
8
7
  readonly x: number;
@@ -17,7 +16,7 @@ declare class Cell {
17
16
  equals(other: Cell): boolean;
18
17
  hasTile(): boolean;
19
18
  isCandidate(): boolean;
20
- toJson(): CellJson | null;
19
+ toJson(): CellJson;
21
20
  toString(): string;
22
21
  }
23
22
  export default Cell;
package/build/Cell.js CHANGED
@@ -13,9 +13,6 @@ class Cell {
13
13
  this.y = y;
14
14
  }
15
15
  static fromJson(json) {
16
- if (!json) {
17
- return Cell.Null;
18
- }
19
16
  return new Cell({
20
17
  isEmpty: json.isEmpty,
21
18
  tile: Tile_1.default.fromJson(json.tile),
@@ -52,16 +49,4 @@ class Cell {
52
49
  return this.tile.toString();
53
50
  }
54
51
  }
55
- Cell.Null = Object.freeze({
56
- isEmpty: true,
57
- tile: Tile_1.default.Null,
58
- x: 0,
59
- y: 0,
60
- clone: () => Cell.Null,
61
- equals: (other) => other === Cell.Null,
62
- hasTile: () => false,
63
- isCandidate: () => false,
64
- toString: () => '',
65
- toJson: () => null,
66
- });
67
52
  exports.default = Cell;
@@ -5,4 +5,5 @@ interface CellJson {
5
5
  x: number;
6
6
  y: number;
7
7
  }
8
+ export declare const isCellJson: (value: unknown) => value is CellJson;
8
9
  export default CellJson;
package/build/CellJson.js CHANGED
@@ -1,2 +1,16 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isCellJson = void 0;
7
+ const isObject_1 = __importDefault(require("./isObject"));
8
+ const TileJson_1 = require("./TileJson");
9
+ const isCellJson = (value) => {
10
+ return ((0, isObject_1.default)(value) &&
11
+ typeof value.isEmpty === 'boolean' &&
12
+ ((0, TileJson_1.isTileJson)(value.tile) || value.tile === null) &&
13
+ typeof value.x === 'number' &&
14
+ typeof value.y === 'number');
15
+ };
16
+ exports.isCellJson = isCellJson;
@@ -0,0 +1,3 @@
1
+ import Cell from './Cell';
2
+ declare type Collision = Cell[];
3
+ export default Collision;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import CellJson from './CellJson';
2
+ declare type CollisionJson = CellJson[];
3
+ export default CollisionJson;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/build/Config.d.ts CHANGED
@@ -13,6 +13,7 @@ declare class Config {
13
13
  get allTilesBonusScore(): number;
14
14
  get alphabet(): string[];
15
15
  get blankScore(): number;
16
+ get blanksCount(): number;
16
17
  get boardHeight(): number;
17
18
  get boardWidth(): number;
18
19
  get twoCharacterTiles(): string[];
@@ -23,8 +24,7 @@ declare class Config {
23
24
  getTilePoints(tile: Tile | null): number | undefined;
24
25
  hasCharacter(character: string): boolean;
25
26
  isTwoCharacterTilePrefix(character: string): boolean;
26
- get maximumNumberOfCharacters(): number;
27
- get numberOfBlanks(): number;
27
+ get maximumCharactersCount(): number;
28
28
  get tiles(): TileConfig[];
29
29
  toJson(): ConfigJson;
30
30
  }
package/build/Config.js CHANGED
@@ -24,6 +24,9 @@ class Config {
24
24
  get blankScore() {
25
25
  return this.config.blankScore;
26
26
  }
27
+ get blanksCount() {
28
+ return this.config.blanksCount;
29
+ }
27
30
  get boardHeight() {
28
31
  return this.config.boardHeight;
29
32
  }
@@ -66,11 +69,8 @@ class Config {
66
69
  isTwoCharacterTilePrefix(character) {
67
70
  return typeof this.getTwoCharacterTileByPrefix(character) !== 'undefined';
68
71
  }
69
- get maximumNumberOfCharacters() {
70
- return this.config.maximumNumberOfCharacters;
71
- }
72
- get numberOfBlanks() {
73
- return this.config.numberOfBlanks;
72
+ get maximumCharactersCount() {
73
+ return this.config.maximumCharactersCount;
74
74
  }
75
75
  get tiles() {
76
76
  return this.config.tiles;
@@ -3,13 +3,13 @@ import TileConfig from './TileConfig';
3
3
  interface ConfigJson {
4
4
  allTilesBonusScore: number;
5
5
  blankScore: number;
6
+ blanksCount: number;
6
7
  boardHeight: number;
7
8
  boardWidth: number;
8
9
  bonuses: BonusJson[];
9
10
  id: string;
10
- maximumNumberOfCharacters: number;
11
+ maximumCharactersCount: number;
11
12
  name: string;
12
- numberOfBlanks: number;
13
13
  tiles: TileConfig[];
14
14
  }
15
15
  export default ConfigJson;
package/build/Locale.d.ts CHANGED
@@ -6,4 +6,5 @@ declare enum Locale {
6
6
  PL_PL = "pl-PL",
7
7
  DE_DE = "de-DE"
8
8
  }
9
+ export declare const isLocale: (locale: unknown) => locale is Locale;
9
10
  export default Locale;
package/build/Locale.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isLocale = void 0;
3
4
  // eslint-disable-next-line no-shadow
4
5
  var Locale;
5
6
  (function (Locale) {
@@ -10,4 +11,7 @@ var Locale;
10
11
  Locale["PL_PL"] = "pl-PL";
11
12
  Locale["DE_DE"] = "de-DE";
12
13
  })(Locale || (Locale = {}));
14
+ const locales = Object.values(Locale);
15
+ const isLocale = (locale) => locales.includes(locale);
16
+ exports.isLocale = isLocale;
13
17
  exports.default = Locale;
@@ -13,7 +13,7 @@ declare class Pattern {
13
13
  clone(): Pattern;
14
14
  collides(): boolean;
15
15
  getIndexOfFirstCellWithoutTile(): number;
16
- getNumberOfEmptyCells(): number;
16
+ getEmptyCellsCount(): number;
17
17
  goesThroughBoardCenter(): boolean;
18
18
  hasAtLeast1EmptyCell(): boolean;
19
19
  hasAtLeast1NonEmptyCell(): boolean;
package/build/Pattern.js CHANGED
@@ -6,9 +6,9 @@ class Pattern {
6
6
  this.cells = cells;
7
7
  }
8
8
  canBePlaced(config) {
9
- const numberOfEmptyCells = this.getNumberOfEmptyCells();
10
- const isNumberOfUsedCellsInRange = numberOfEmptyCells >= 1 && numberOfEmptyCells <= config.maximumNumberOfCharacters;
11
- return (isNumberOfUsedCellsInRange &&
9
+ const emptyCellsCount = this.getEmptyCellsCount();
10
+ const isUsedCellsCountInRange = emptyCellsCount >= 1 && emptyCellsCount <= config.maximumCharactersCount;
11
+ return (isUsedCellsCountInRange &&
12
12
  (this.hasAtLeast1NonEmptyCell() || this.collides() || (this.goesThroughBoardCenter() && this.board.isEmpty())));
13
13
  }
14
14
  clone() {
@@ -23,7 +23,7 @@ class Pattern {
23
23
  getIndexOfFirstCellWithoutTile() {
24
24
  return this.cells.findIndex((cell) => !cell.hasTile());
25
25
  }
26
- getNumberOfEmptyCells() {
26
+ getEmptyCellsCount() {
27
27
  return this.cells.filter((cell) => cell.isEmpty).length;
28
28
  }
29
29
  goesThroughBoardCenter() {
@@ -1,6 +1,6 @@
1
1
  import CellJson from './CellJson';
2
2
  interface PatternJson {
3
- cells: (CellJson | null)[];
3
+ cells: CellJson[];
4
4
  collisions: PatternJson[];
5
5
  word: string;
6
6
  }
package/build/Result.d.ts CHANGED
@@ -1,26 +1,28 @@
1
1
  import Cell from './Cell';
2
2
  import ResultJson from './ResultJson';
3
3
  import Tile from './Tile';
4
+ declare type Collision = Cell[];
4
5
  declare class Result {
5
6
  static fromJson(json: ResultJson): Result;
7
+ readonly blanksCount: number;
6
8
  readonly cells: Cell[];
9
+ readonly collisions: Collision[];
10
+ readonly consonantsCount: number;
7
11
  readonly id: number;
8
12
  readonly length: number;
9
- readonly numberOfBlanks: number;
10
- readonly numberOfCollisions: number;
11
- readonly numberOfConsonants: number;
12
- readonly numberOfTiles: number;
13
- readonly numberOfVowels: number;
14
- readonly numberOfWords: number;
15
13
  readonly points: number;
16
14
  readonly pointsRatio: number;
17
15
  readonly tiles: Tile[];
18
16
  readonly tilesCharacters: string;
17
+ readonly tilesCount: number;
18
+ readonly vowelsCount: number;
19
19
  readonly word: string;
20
- constructor({ cells, id, numberOfCollisions, points, }: {
20
+ readonly words: string[];
21
+ readonly wordsCount: number;
22
+ constructor({ cells, id, collisions, points, }: {
21
23
  cells: Cell[];
22
24
  id: number;
23
- numberOfCollisions: number;
25
+ collisions: Collision[];
24
26
  points: number;
25
27
  });
26
28
  toJson(): ResultJson;
package/build/Result.js CHANGED
@@ -6,28 +6,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const constants_1 = require("@scrabble-solver/constants");
7
7
  const Cell_1 = __importDefault(require("./Cell"));
8
8
  class Result {
9
- constructor({ cells, id, numberOfCollisions, points, }) {
9
+ constructor({ cells, id, collisions, points, }) {
10
10
  const tiles = getTiles(cells);
11
+ this.blanksCount = getBlanks(tiles).length;
11
12
  this.cells = cells;
13
+ this.collisions = collisions;
14
+ this.consonantsCount = getConsonants(tiles).length;
12
15
  this.id = id;
13
16
  this.length = cells.length;
14
- this.numberOfCollisions = numberOfCollisions;
15
- this.numberOfConsonants = getConsonants(tiles).length;
16
- this.numberOfBlanks = getBlanks(tiles).length;
17
- this.numberOfTiles = tiles.length;
18
- this.numberOfVowels = getVowels(tiles).length;
19
- this.numberOfWords = 1 + this.numberOfCollisions;
20
17
  this.points = points;
21
18
  this.pointsRatio = getPointsRatio(tiles, points);
22
19
  this.tiles = tiles;
23
20
  this.tilesCharacters = getTilesCharacters(tiles);
21
+ this.tilesCount = tiles.length;
22
+ this.vowelsCount = getVowels(tiles).length;
24
23
  this.word = getWord(cells);
24
+ this.words = getWords(cells, collisions);
25
+ this.wordsCount = 1 + this.collisions.length;
25
26
  }
26
27
  static fromJson(json) {
27
28
  return new Result({
28
- cells: json.cells.map(Cell_1.default.fromJson),
29
29
  id: json.id,
30
- numberOfCollisions: json.numberOfCollisions,
30
+ cells: json.cells.map(Cell_1.default.fromJson),
31
+ collisions: json.collisions.map((collision) => collision.map(Cell_1.default.fromJson)),
31
32
  points: json.points,
32
33
  });
33
34
  }
@@ -35,7 +36,7 @@ class Result {
35
36
  return {
36
37
  cells: this.cells.map((cell) => cell.toJson()),
37
38
  id: this.id,
38
- numberOfCollisions: this.numberOfCollisions,
39
+ collisions: this.collisions.map((collision) => collision.map((cell) => cell.toJson())),
39
40
  points: this.points,
40
41
  };
41
42
  }
@@ -50,6 +51,10 @@ const getPointsRatio = (tiles, points) => points / tiles.length;
50
51
  const getTiles = (cells) => cells.filter(({ isEmpty }) => isEmpty).map(({ tile }) => tile);
51
52
  const getTilesCharacters = (tiles) => getNonBlankCharacters(tiles).sort(charactersComparator).join('');
52
53
  const getWord = (cells) => cells.map(String).join('');
54
+ const getWords = (cells, collisions) => [
55
+ getWord(cells),
56
+ ...collisions.map((collision) => collision.map(String).join('')),
57
+ ];
53
58
  const isConsonant = ({ character, isBlank }) => constants_1.CONSONANTS.includes(character) && !isBlank;
54
59
  const isVowel = ({ character, isBlank }) => constants_1.VOWELS.includes(character) && !isBlank;
55
60
  exports.default = Result;
@@ -1,8 +1,9 @@
1
1
  import CellJson from './CellJson';
2
+ import CollisionJson from './CollisionJson';
2
3
  interface ResultJson {
3
- cells: (CellJson | null)[];
4
+ cells: CellJson[];
5
+ collisions: CollisionJson[];
4
6
  id: number;
5
- numberOfCollisions: number;
6
7
  points: number;
7
8
  }
8
9
  export default ResultJson;
@@ -2,4 +2,5 @@ interface TileJson {
2
2
  character: string;
3
3
  isBlank: boolean;
4
4
  }
5
+ export declare const isTileJson: (value: unknown) => value is TileJson;
5
6
  export default TileJson;
package/build/TileJson.js CHANGED
@@ -1,2 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isTileJson = void 0;
7
+ const isObject_1 = __importDefault(require("./isObject"));
8
+ const isTileJson = (value) => {
9
+ return (0, isObject_1.default)(value) && typeof value.character === 'string' && typeof value.isBlank === 'boolean';
10
+ };
11
+ exports.isTileJson = isTileJson;
package/build/index.d.ts CHANGED
@@ -1,22 +1,25 @@
1
1
  export { default as Board } from './Board';
2
- export { default as BoardJson } from './BoardJson';
2
+ export { default as BoardJson, isBoardJson } from './BoardJson';
3
3
  export { default as Bonus } from './Bonus';
4
4
  export { default as BonusJson } from './BonusJson';
5
5
  export { default as BonusValue } from './BonusValue';
6
6
  export { default as Cell } from './Cell';
7
- export { default as CellJson } from './CellJson';
7
+ export { default as CellJson, isCellJson } from './CellJson';
8
8
  export { default as CharacterBonus } from './CharacterBonus';
9
+ export { default as Collision } from './Collision';
10
+ export { default as CollisionJson } from './CollisionJson';
9
11
  export { default as Config } from './Config';
10
12
  export { default as ConfigJson } from './ConfigJson';
11
13
  export { default as HorizontalPattern } from './HorizontalPattern';
12
- export { default as Locale } from './Locale';
14
+ export { default as isObject } from './isObject';
15
+ export { default as Locale, isLocale } from './Locale';
13
16
  export { default as Pattern } from './Pattern';
14
17
  export { default as PatternJson } from './PatternJson';
15
18
  export { default as Result } from './Result';
16
19
  export { default as ResultJson } from './ResultJson';
17
20
  export { default as Tile } from './Tile';
18
21
  export { default as TileConfig } from './TileConfig';
19
- export { default as TileJson } from './TileJson';
22
+ export { default as TileJson, isTileJson } from './TileJson';
20
23
  export { default as VerticalPattern } from './VerticalPattern';
21
24
  export { default as WordBonus } from './WordBonus';
22
25
  export { default as WordDefinition } from './WordDefinition';
package/build/index.js CHANGED
@@ -3,27 +3,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WordDefinition = exports.WordBonus = exports.VerticalPattern = exports.Tile = exports.Result = exports.Pattern = exports.Locale = exports.HorizontalPattern = exports.Config = exports.CharacterBonus = exports.Cell = exports.Bonus = exports.Board = void 0;
6
+ exports.WordDefinition = exports.WordBonus = exports.VerticalPattern = exports.isTileJson = exports.Tile = exports.Result = exports.Pattern = exports.isLocale = exports.Locale = exports.isObject = exports.HorizontalPattern = exports.Config = exports.CharacterBonus = exports.isCellJson = exports.Cell = exports.Bonus = exports.isBoardJson = exports.Board = void 0;
7
7
  var Board_1 = require("./Board");
8
8
  Object.defineProperty(exports, "Board", { enumerable: true, get: function () { return __importDefault(Board_1).default; } });
9
+ var BoardJson_1 = require("./BoardJson");
10
+ Object.defineProperty(exports, "isBoardJson", { enumerable: true, get: function () { return BoardJson_1.isBoardJson; } });
9
11
  var Bonus_1 = require("./Bonus");
10
12
  Object.defineProperty(exports, "Bonus", { enumerable: true, get: function () { return __importDefault(Bonus_1).default; } });
11
13
  var Cell_1 = require("./Cell");
12
14
  Object.defineProperty(exports, "Cell", { enumerable: true, get: function () { return __importDefault(Cell_1).default; } });
15
+ var CellJson_1 = require("./CellJson");
16
+ Object.defineProperty(exports, "isCellJson", { enumerable: true, get: function () { return CellJson_1.isCellJson; } });
13
17
  var CharacterBonus_1 = require("./CharacterBonus");
14
18
  Object.defineProperty(exports, "CharacterBonus", { enumerable: true, get: function () { return __importDefault(CharacterBonus_1).default; } });
15
19
  var Config_1 = require("./Config");
16
20
  Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return __importDefault(Config_1).default; } });
17
21
  var HorizontalPattern_1 = require("./HorizontalPattern");
18
22
  Object.defineProperty(exports, "HorizontalPattern", { enumerable: true, get: function () { return __importDefault(HorizontalPattern_1).default; } });
23
+ var isObject_1 = require("./isObject");
24
+ Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return __importDefault(isObject_1).default; } });
19
25
  var Locale_1 = require("./Locale");
20
26
  Object.defineProperty(exports, "Locale", { enumerable: true, get: function () { return __importDefault(Locale_1).default; } });
27
+ Object.defineProperty(exports, "isLocale", { enumerable: true, get: function () { return Locale_1.isLocale; } });
21
28
  var Pattern_1 = require("./Pattern");
22
29
  Object.defineProperty(exports, "Pattern", { enumerable: true, get: function () { return __importDefault(Pattern_1).default; } });
23
30
  var Result_1 = require("./Result");
24
31
  Object.defineProperty(exports, "Result", { enumerable: true, get: function () { return __importDefault(Result_1).default; } });
25
32
  var Tile_1 = require("./Tile");
26
33
  Object.defineProperty(exports, "Tile", { enumerable: true, get: function () { return __importDefault(Tile_1).default; } });
34
+ var TileJson_1 = require("./TileJson");
35
+ Object.defineProperty(exports, "isTileJson", { enumerable: true, get: function () { return TileJson_1.isTileJson; } });
27
36
  var VerticalPattern_1 = require("./VerticalPattern");
28
37
  Object.defineProperty(exports, "VerticalPattern", { enumerable: true, get: function () { return __importDefault(VerticalPattern_1).default; } });
29
38
  var WordBonus_1 = require("./WordBonus");
@@ -0,0 +1,2 @@
1
+ declare const isObject: (value: unknown) => value is Record<string, unknown>;
2
+ export default isObject;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const isObject = (value) => {
4
+ return typeof value === 'object' && value !== null;
5
+ };
6
+ exports.default = isObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scrabble-solver/types",
3
- "version": "2.8.6",
3
+ "version": "2.8.8",
4
4
  "description": "Scrabble Solver 2 - Types",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  "clean:force": "npm run clean && rimraf package-lock.json"
25
25
  },
26
26
  "dependencies": {
27
- "@scrabble-solver/constants": "^2.8.6"
27
+ "@scrabble-solver/constants": "^2.8.8"
28
28
  },
29
- "gitHead": "ec130aa77ac64b91ab82e79f130eb69ac561fffa"
29
+ "gitHead": "e6ced6e98204e49ba69750242643bda52a453635"
30
30
  }
package/src/Board.ts CHANGED
@@ -31,21 +31,21 @@ class Board {
31
31
  });
32
32
  }
33
33
 
34
- public readonly rows: Cell[][];
34
+ public readonly columnsCount: number;
35
35
 
36
- public readonly numberOfColumns: number;
36
+ public readonly rows: Cell[][];
37
37
 
38
- public readonly numberOfRows: number;
38
+ public readonly rowsCount: number;
39
39
 
40
40
  constructor({ rows }: { rows: Cell[][] }) {
41
41
  this.rows = rows;
42
- this.numberOfColumns = rows[0].length;
43
- this.numberOfRows = rows.length;
42
+ this.columnsCount = rows[0].length;
43
+ this.rowsCount = rows.length;
44
44
  }
45
45
 
46
46
  public get center(): Cell {
47
- const x = Math.floor(this.numberOfColumns / 2);
48
- const y = Math.floor(this.numberOfRows / 2);
47
+ const x = Math.floor(this.columnsCount / 2);
48
+ const y = Math.floor(this.rowsCount / 2);
49
49
  return this.rows[y][x];
50
50
  }
51
51
 
@@ -59,7 +59,7 @@ class Board {
59
59
  }
60
60
 
61
61
  public collidesDown({ x, y }: Cell): boolean {
62
- return y < this.numberOfRows - 1 && !this.rows[y + 1][x].isEmpty;
62
+ return y < this.rowsCount - 1 && !this.rows[y + 1][x].isEmpty;
63
63
  }
64
64
 
65
65
  public collidesLeft({ x, y }: Cell): boolean {
@@ -67,7 +67,7 @@ class Board {
67
67
  }
68
68
 
69
69
  public collidesRight({ x, y }: Cell): boolean {
70
- return x < this.numberOfColumns - 1 && !this.rows[y][x + 1].isEmpty;
70
+ return x < this.columnsCount - 1 && !this.rows[y][x + 1].isEmpty;
71
71
  }
72
72
 
73
73
  public collidesUp({ x, y }: Cell): boolean {
@@ -76,8 +76,8 @@ class Board {
76
76
 
77
77
  public equals(other: Board): boolean {
78
78
  return (
79
- this.numberOfColumns === other.numberOfColumns &&
80
- this.numberOfRows === other.numberOfRows &&
79
+ this.columnsCount === other.columnsCount &&
80
+ this.rowsCount === other.rowsCount &&
81
81
  this.rows.every((row, rowIndex) => {
82
82
  return row.every((cell, cellIndex) => {
83
83
  return cell.equals(other.rows[rowIndex][cellIndex]);
package/src/BoardJson.ts CHANGED
@@ -1,5 +1,25 @@
1
- import CellJson from './CellJson';
1
+ import CellJson, { isCellJson } from './CellJson';
2
2
 
3
- type BoardJson = (CellJson | null)[][];
3
+ type BoardJson = CellJson[][];
4
+
5
+ export const isBoardJson = (value: unknown): value is BoardJson => {
6
+ if (!Array.isArray(value)) {
7
+ return false;
8
+ }
9
+
10
+ for (const row of value) {
11
+ if (!Array.isArray(value)) {
12
+ return false;
13
+ }
14
+
15
+ for (const cell of row) {
16
+ if (!isCellJson(cell)) {
17
+ return false;
18
+ }
19
+ }
20
+ }
21
+
22
+ return true;
23
+ };
4
24
 
5
25
  export default BoardJson;
package/src/Cell.ts CHANGED
@@ -2,11 +2,7 @@ import CellJson from './CellJson';
2
2
  import Tile from './Tile';
3
3
 
4
4
  class Cell {
5
- public static fromJson(json: CellJson | null): Cell {
6
- if (!json) {
7
- return Cell.Null;
8
- }
9
-
5
+ public static fromJson(json: CellJson): Cell {
10
6
  return new Cell({
11
7
  isEmpty: json.isEmpty,
12
8
  tile: Tile.fromJson(json.tile),
@@ -15,19 +11,6 @@ class Cell {
15
11
  });
16
12
  }
17
13
 
18
- public static readonly Null: Cell = Object.freeze({
19
- isEmpty: true,
20
- tile: Tile.Null,
21
- x: 0,
22
- y: 0,
23
- clone: () => Cell.Null,
24
- equals: (other: Cell) => other === Cell.Null,
25
- hasTile: () => false,
26
- isCandidate: () => false,
27
- toString: () => '',
28
- toJson: () => null,
29
- });
30
-
31
14
  public readonly isEmpty: boolean;
32
15
 
33
16
  public tile: Tile;
@@ -65,7 +48,7 @@ class Cell {
65
48
  return this.isEmpty && this.hasTile();
66
49
  }
67
50
 
68
- public toJson(): CellJson | null {
51
+ public toJson(): CellJson {
69
52
  return {
70
53
  isEmpty: this.isEmpty,
71
54
  tile: this.tile.toJson(),
package/src/CellJson.ts CHANGED
@@ -1,4 +1,5 @@
1
- import TileJson from './TileJson';
1
+ import isObject from './isObject';
2
+ import TileJson, { isTileJson } from './TileJson';
2
3
 
3
4
  interface CellJson {
4
5
  isEmpty: boolean;
@@ -7,4 +8,14 @@ interface CellJson {
7
8
  y: number;
8
9
  }
9
10
 
11
+ export const isCellJson = (value: unknown): value is CellJson => {
12
+ return (
13
+ isObject(value) &&
14
+ typeof value.isEmpty === 'boolean' &&
15
+ (isTileJson(value.tile) || value.tile === null) &&
16
+ typeof value.x === 'number' &&
17
+ typeof value.y === 'number'
18
+ );
19
+ };
20
+
10
21
  export default CellJson;
@@ -0,0 +1,5 @@
1
+ import Cell from './Cell';
2
+
3
+ type Collision = Cell[];
4
+
5
+ export default Collision;
@@ -0,0 +1,5 @@
1
+ import CellJson from './CellJson';
2
+
3
+ type CollisionJson = CellJson[];
4
+
5
+ export default CollisionJson;
package/src/Config.ts CHANGED
@@ -38,6 +38,10 @@ class Config {
38
38
  return this.config.blankScore;
39
39
  }
40
40
 
41
+ public get blanksCount(): number {
42
+ return this.config.blanksCount;
43
+ }
44
+
41
45
  public get boardHeight(): number {
42
46
  return this.config.boardHeight;
43
47
  }
@@ -94,12 +98,8 @@ class Config {
94
98
  return typeof this.getTwoCharacterTileByPrefix(character) !== 'undefined';
95
99
  }
96
100
 
97
- public get maximumNumberOfCharacters(): number {
98
- return this.config.maximumNumberOfCharacters;
99
- }
100
-
101
- public get numberOfBlanks(): number {
102
- return this.config.numberOfBlanks;
101
+ public get maximumCharactersCount(): number {
102
+ return this.config.maximumCharactersCount;
103
103
  }
104
104
 
105
105
  public get tiles(): TileConfig[] {
package/src/ConfigJson.ts CHANGED
@@ -4,13 +4,13 @@ import TileConfig from './TileConfig';
4
4
  interface ConfigJson {
5
5
  allTilesBonusScore: number;
6
6
  blankScore: number;
7
+ blanksCount: number;
7
8
  boardHeight: number;
8
9
  boardWidth: number;
9
10
  bonuses: BonusJson[];
10
11
  id: string;
11
- maximumNumberOfCharacters: number;
12
+ maximumCharactersCount: number;
12
13
  name: string;
13
- numberOfBlanks: number;
14
14
  tiles: TileConfig[];
15
15
  }
16
16
 
package/src/Locale.ts CHANGED
@@ -8,4 +8,8 @@ enum Locale {
8
8
  DE_DE = 'de-DE',
9
9
  }
10
10
 
11
+ const locales = Object.values(Locale);
12
+
13
+ export const isLocale = (locale: unknown): locale is Locale => locales.includes(locale as Locale);
14
+
11
15
  export default Locale;
package/src/Pattern.ts CHANGED
@@ -14,11 +14,10 @@ class Pattern {
14
14
  }
15
15
 
16
16
  public canBePlaced(config: Config): boolean {
17
- const numberOfEmptyCells = this.getNumberOfEmptyCells();
18
- const isNumberOfUsedCellsInRange =
19
- numberOfEmptyCells >= 1 && numberOfEmptyCells <= config.maximumNumberOfCharacters;
17
+ const emptyCellsCount = this.getEmptyCellsCount();
18
+ const isUsedCellsCountInRange = emptyCellsCount >= 1 && emptyCellsCount <= config.maximumCharactersCount;
20
19
  return (
21
- isNumberOfUsedCellsInRange &&
20
+ isUsedCellsCountInRange &&
22
21
  (this.hasAtLeast1NonEmptyCell() || this.collides() || (this.goesThroughBoardCenter() && this.board.isEmpty()))
23
22
  );
24
23
  }
@@ -38,7 +37,7 @@ class Pattern {
38
37
  return this.cells.findIndex((cell) => !cell.hasTile());
39
38
  }
40
39
 
41
- public getNumberOfEmptyCells(): number {
40
+ public getEmptyCellsCount(): number {
42
41
  return this.cells.filter((cell) => cell.isEmpty).length;
43
42
  }
44
43
 
@@ -1,7 +1,7 @@
1
1
  import CellJson from './CellJson';
2
2
 
3
3
  interface PatternJson {
4
- cells: (CellJson | null)[];
4
+ cells: CellJson[];
5
5
  collisions: PatternJson[];
6
6
  word: string;
7
7
  }
package/src/Result.ts CHANGED
@@ -4,33 +4,29 @@ import Cell from './Cell';
4
4
  import ResultJson from './ResultJson';
5
5
  import Tile from './Tile';
6
6
 
7
+ type Collision = Cell[];
8
+
7
9
  class Result {
8
10
  public static fromJson(json: ResultJson): Result {
9
11
  return new Result({
10
- cells: json.cells.map(Cell.fromJson),
11
12
  id: json.id,
12
- numberOfCollisions: json.numberOfCollisions,
13
+ cells: json.cells.map(Cell.fromJson),
14
+ collisions: json.collisions.map((collision) => collision.map(Cell.fromJson)),
13
15
  points: json.points,
14
16
  });
15
17
  }
16
18
 
17
- public readonly cells: Cell[];
18
-
19
- public readonly id: number;
20
-
21
- public readonly length: number;
22
-
23
- public readonly numberOfBlanks: number;
19
+ public readonly blanksCount: number;
24
20
 
25
- public readonly numberOfCollisions: number;
21
+ public readonly cells: Cell[];
26
22
 
27
- public readonly numberOfConsonants: number;
23
+ public readonly collisions: Collision[];
28
24
 
29
- public readonly numberOfTiles: number;
25
+ public readonly consonantsCount: number;
30
26
 
31
- public readonly numberOfVowels: number;
27
+ public readonly id: number;
32
28
 
33
- public readonly numberOfWords: number;
29
+ public readonly length: number;
34
30
 
35
31
  public readonly points: number;
36
32
 
@@ -40,41 +36,50 @@ class Result {
40
36
 
41
37
  public readonly tilesCharacters: string;
42
38
 
39
+ public readonly tilesCount: number;
40
+
41
+ public readonly vowelsCount: number;
42
+
43
43
  public readonly word: string;
44
44
 
45
+ public readonly words: string[];
46
+
47
+ public readonly wordsCount: number;
48
+
45
49
  constructor({
46
50
  cells,
47
51
  id,
48
- numberOfCollisions,
52
+ collisions,
49
53
  points,
50
54
  }: {
51
55
  cells: Cell[];
52
56
  id: number;
53
- numberOfCollisions: number;
57
+ collisions: Collision[];
54
58
  points: number;
55
59
  }) {
56
60
  const tiles = getTiles(cells);
61
+ this.blanksCount = getBlanks(tiles).length;
57
62
  this.cells = cells;
63
+ this.collisions = collisions;
64
+ this.consonantsCount = getConsonants(tiles).length;
58
65
  this.id = id;
59
66
  this.length = cells.length;
60
- this.numberOfCollisions = numberOfCollisions;
61
- this.numberOfConsonants = getConsonants(tiles).length;
62
- this.numberOfBlanks = getBlanks(tiles).length;
63
- this.numberOfTiles = tiles.length;
64
- this.numberOfVowels = getVowels(tiles).length;
65
- this.numberOfWords = 1 + this.numberOfCollisions;
66
67
  this.points = points;
67
68
  this.pointsRatio = getPointsRatio(tiles, points);
68
69
  this.tiles = tiles;
69
70
  this.tilesCharacters = getTilesCharacters(tiles);
71
+ this.tilesCount = tiles.length;
72
+ this.vowelsCount = getVowels(tiles).length;
70
73
  this.word = getWord(cells);
74
+ this.words = getWords(cells, collisions);
75
+ this.wordsCount = 1 + this.collisions.length;
71
76
  }
72
77
 
73
78
  public toJson(): ResultJson {
74
79
  return {
75
80
  cells: this.cells.map((cell) => cell.toJson()),
76
81
  id: this.id,
77
- numberOfCollisions: this.numberOfCollisions,
82
+ collisions: this.collisions.map((collision) => collision.map((cell) => cell.toJson())),
78
83
  points: this.points,
79
84
  };
80
85
  }
@@ -100,6 +105,11 @@ const getTilesCharacters = (tiles: Tile[]): string => getNonBlankCharacters(tile
100
105
 
101
106
  const getWord = (cells: Cell[]): string => cells.map(String).join('');
102
107
 
108
+ const getWords = (cells: Cell[], collisions: Collision[]): string[] => [
109
+ getWord(cells),
110
+ ...collisions.map((collision) => collision.map(String).join('')),
111
+ ];
112
+
103
113
  const isConsonant = ({ character, isBlank }: Tile): boolean => CONSONANTS.includes(character) && !isBlank;
104
114
 
105
115
  const isVowel = ({ character, isBlank }: Tile): boolean => VOWELS.includes(character) && !isBlank;
package/src/ResultJson.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import CellJson from './CellJson';
2
+ import CollisionJson from './CollisionJson';
2
3
 
3
4
  interface ResultJson {
4
- cells: (CellJson | null)[];
5
+ cells: CellJson[];
6
+ collisions: CollisionJson[];
5
7
  id: number;
6
- numberOfCollisions: number;
7
8
  points: number;
8
9
  }
9
10
 
package/src/TileJson.ts CHANGED
@@ -1,6 +1,12 @@
1
+ import isObject from './isObject';
2
+
1
3
  interface TileJson {
2
4
  character: string;
3
5
  isBlank: boolean;
4
6
  }
5
7
 
8
+ export const isTileJson = (value: unknown): value is TileJson => {
9
+ return isObject(value) && typeof value.character === 'string' && typeof value.isBlank === 'boolean';
10
+ };
11
+
6
12
  export default TileJson;
package/src/index.ts CHANGED
@@ -1,22 +1,25 @@
1
1
  export { default as Board } from './Board';
2
- export { default as BoardJson } from './BoardJson';
2
+ export { default as BoardJson, isBoardJson } from './BoardJson';
3
3
  export { default as Bonus } from './Bonus';
4
4
  export { default as BonusJson } from './BonusJson';
5
5
  export { default as BonusValue } from './BonusValue';
6
6
  export { default as Cell } from './Cell';
7
- export { default as CellJson } from './CellJson';
7
+ export { default as CellJson, isCellJson } from './CellJson';
8
8
  export { default as CharacterBonus } from './CharacterBonus';
9
+ export { default as Collision } from './Collision';
10
+ export { default as CollisionJson } from './CollisionJson';
9
11
  export { default as Config } from './Config';
10
12
  export { default as ConfigJson } from './ConfigJson';
11
13
  export { default as HorizontalPattern } from './HorizontalPattern';
12
- export { default as Locale } from './Locale';
14
+ export { default as isObject } from './isObject';
15
+ export { default as Locale, isLocale } from './Locale';
13
16
  export { default as Pattern } from './Pattern';
14
17
  export { default as PatternJson } from './PatternJson';
15
18
  export { default as Result } from './Result';
16
19
  export { default as ResultJson } from './ResultJson';
17
20
  export { default as Tile } from './Tile';
18
21
  export { default as TileConfig } from './TileConfig';
19
- export { default as TileJson } from './TileJson';
22
+ export { default as TileJson, isTileJson } from './TileJson';
20
23
  export { default as VerticalPattern } from './VerticalPattern';
21
24
  export { default as WordBonus } from './WordBonus';
22
25
  export { default as WordDefinition } from './WordDefinition';
@@ -0,0 +1,5 @@
1
+ const isObject = (value: unknown): value is Record<string, unknown> => {
2
+ return typeof value === 'object' && value !== null;
3
+ };
4
+
5
+ export default isObject;