@scrabble-solver/types 2.8.7 → 2.8.9

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
@@ -22,6 +22,7 @@ declare class Board {
22
22
  getColumn(index: number): Cell[];
23
23
  getRow(index: number): Cell[];
24
24
  getTilesCount(): number;
25
+ getWords(): string[];
25
26
  isEmpty(): boolean;
26
27
  toJson(): BoardJson;
27
28
  toString(): string;
package/build/Board.js CHANGED
@@ -79,6 +79,22 @@ class Board {
79
79
  return count + row.reduce((rowCount, cell) => (cell.hasTile() ? rowCount + 1 : rowCount), 0);
80
80
  }, 0);
81
81
  }
82
+ getWords() {
83
+ const columns = [];
84
+ for (let x = 0; x < this.columnsCount; ++x) {
85
+ const column = [];
86
+ for (let y = 0; y < this.rowsCount; ++y) {
87
+ column.push(this.rows[y][x]);
88
+ }
89
+ columns.push(column);
90
+ }
91
+ const columnsBoard = new Board({ rows: columns });
92
+ const lines = this.toString().split('\n').concat(columnsBoard.toString().split('\n'));
93
+ const words = lines
94
+ .flatMap((line) => line.replaceAll(/\s+/g, constants_1.EMPTY_CELL).split(' '))
95
+ .filter((word) => word.length > 1);
96
+ return words;
97
+ }
82
98
  isEmpty() {
83
99
  return this.rows.every((row) => row.every(({ isEmpty }) => isEmpty));
84
100
  }
@@ -86,7 +102,7 @@ class Board {
86
102
  return this.rows.map((row) => row.map((cell) => cell.toJson()));
87
103
  }
88
104
  toString() {
89
- return this.rows.map((row) => row.map(String)).join('\n');
105
+ return this.rows.map((row) => row.map(String).join('')).join('\n');
90
106
  }
91
107
  updateCell(x, y, updateCell) {
92
108
  this.rows[y][x] = updateCell(this.rows[y][x]);
package/build/Config.js CHANGED
@@ -64,7 +64,7 @@ class Config {
64
64
  return tile.isBlank ? this.blankScore : this.getCharacterPoints(tile.character);
65
65
  }
66
66
  hasCharacter(character) {
67
- return this.alphabet.includes(character);
67
+ return character in this.pointsMap;
68
68
  }
69
69
  isTwoCharacterTilePrefix(character) {
70
70
  return typeof this.getTwoCharacterTileByPrefix(character) !== 'undefined';
@@ -0,0 +1,7 @@
1
+ import Pattern from './Pattern';
2
+ declare class FinalPattern extends Pattern {
3
+ private readonly collisions;
4
+ constructor(pattern: Pattern);
5
+ getCollisions(): Pattern[];
6
+ }
7
+ export default FinalPattern;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const Pattern_1 = __importDefault(require("./Pattern"));
7
+ class FinalPattern extends Pattern_1.default {
8
+ constructor(pattern) {
9
+ super(pattern.board, pattern.cells);
10
+ this.collisions = pattern.getCollisions();
11
+ }
12
+ getCollisions() {
13
+ return this.collisions;
14
+ }
15
+ }
16
+ exports.default = FinalPattern;
@@ -6,10 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const Pattern_1 = __importDefault(require("./Pattern"));
7
7
  class HorizontalPattern extends Pattern_1.default {
8
8
  clone() {
9
- return new HorizontalPattern({
10
- board: this.board,
11
- cells: this.cells.map((cell) => cell.clone()),
12
- });
9
+ return new HorizontalPattern(this.board, this.cells.map((cell) => cell.clone()));
13
10
  }
14
11
  getCollisions() {
15
12
  const collisions = [];
@@ -29,7 +26,7 @@ class HorizontalPattern extends Pattern_1.default {
29
26
  const nextCells = column.slice(cell.y + 1, y);
30
27
  const cells = [...previousCells, cell, ...nextCells];
31
28
  if (cells.length > 1) {
32
- const pattern = new Pattern_1.default({ board: this.board, cells });
29
+ const pattern = new Pattern_1.default(this.board, cells);
33
30
  collisions.push(pattern);
34
31
  }
35
32
  });
@@ -5,10 +5,7 @@ import PatternJson from './PatternJson';
5
5
  declare class Pattern {
6
6
  readonly board: Board;
7
7
  readonly cells: Cell[];
8
- constructor({ board, cells }: {
9
- board: Board;
10
- cells: Cell[];
11
- });
8
+ constructor(board: Board, cells: Cell[]);
12
9
  canBePlaced(config: Config): boolean;
13
10
  clone(): Pattern;
14
11
  collides(): boolean;
package/build/Pattern.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class Pattern {
4
- constructor({ board, cells }) {
4
+ constructor(board, cells) {
5
5
  this.board = board;
6
6
  this.cells = cells;
7
7
  }
@@ -12,10 +12,7 @@ class Pattern {
12
12
  (this.hasAtLeast1NonEmptyCell() || this.collides() || (this.goesThroughBoardCenter() && this.board.isEmpty())));
13
13
  }
14
14
  clone() {
15
- return new Pattern({
16
- board: this.board,
17
- cells: this.cells.map((cell) => cell.clone()),
18
- });
15
+ return new Pattern(this.board, this.cells.map((cell) => cell.clone()));
19
16
  }
20
17
  collides() {
21
18
  return this.cells.some((cell) => cell.isEmpty && this.board.collides(cell));
package/build/Result.js CHANGED
@@ -50,11 +50,9 @@ const getNonBlanks = (tiles) => tiles.filter(({ isBlank }) => !isBlank);
50
50
  const getPointsRatio = (tiles, points) => points / tiles.length;
51
51
  const getTiles = (cells) => cells.filter(({ isEmpty }) => isEmpty).map(({ tile }) => tile);
52
52
  const getTilesCharacters = (tiles) => getNonBlankCharacters(tiles).sort(charactersComparator).join('');
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
+ // eslint-disable-next-line prefer-template
54
+ const getWord = (cells) => cells.reduce((word, cell) => word + cell.toString(), '');
55
+ const getWords = (cells, collisions) => [cells, ...collisions].map(getWord);
58
56
  const isConsonant = ({ character, isBlank }) => constants_1.CONSONANTS.includes(character) && !isBlank;
59
57
  const isVowel = ({ character, isBlank }) => constants_1.VOWELS.includes(character) && !isBlank;
60
58
  exports.default = Result;
package/build/Tile.d.ts CHANGED
@@ -2,8 +2,8 @@ import TileJson from './TileJson';
2
2
  declare class Tile {
3
3
  static fromJson(json: TileJson | null): Tile;
4
4
  static readonly Null: Tile;
5
- readonly character: string;
6
- readonly isBlank: boolean;
5
+ character: string;
6
+ isBlank: boolean;
7
7
  constructor({ character, isBlank }: {
8
8
  character: string;
9
9
  isBlank?: boolean;
@@ -6,10 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const Pattern_1 = __importDefault(require("./Pattern"));
7
7
  class VerticalPattern extends Pattern_1.default {
8
8
  clone() {
9
- return new VerticalPattern({
10
- board: this.board,
11
- cells: this.cells.map((cell) => cell.clone()),
12
- });
9
+ return new VerticalPattern(this.board, this.cells.map((cell) => cell.clone()));
13
10
  }
14
11
  getCollisions() {
15
12
  const collisions = [];
@@ -29,7 +26,7 @@ class VerticalPattern extends Pattern_1.default {
29
26
  const nextCells = row.slice(cell.x + 1, x);
30
27
  const cells = [...previousCells, cell, ...nextCells];
31
28
  if (cells.length > 1) {
32
- const pattern = new Pattern_1.default({ board: this.board, cells });
29
+ const pattern = new Pattern_1.default(this.board, cells);
33
30
  collisions.push(pattern);
34
31
  }
35
32
  });
package/build/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export { default as Collision } from './Collision';
10
10
  export { default as CollisionJson } from './CollisionJson';
11
11
  export { default as Config } from './Config';
12
12
  export { default as ConfigJson } from './ConfigJson';
13
+ export { default as FinalPattern } from './FinalPattern';
13
14
  export { default as HorizontalPattern } from './HorizontalPattern';
14
15
  export { default as isObject } from './isObject';
15
16
  export { default as Locale, isLocale } from './Locale';
package/build/index.js CHANGED
@@ -3,7 +3,7 @@ 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.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;
6
+ exports.WordDefinition = exports.WordBonus = exports.VerticalPattern = exports.isTileJson = exports.Tile = exports.Result = exports.Pattern = exports.isLocale = exports.Locale = exports.isObject = exports.HorizontalPattern = exports.FinalPattern = 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
9
  var BoardJson_1 = require("./BoardJson");
@@ -18,6 +18,8 @@ var CharacterBonus_1 = require("./CharacterBonus");
18
18
  Object.defineProperty(exports, "CharacterBonus", { enumerable: true, get: function () { return __importDefault(CharacterBonus_1).default; } });
19
19
  var Config_1 = require("./Config");
20
20
  Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return __importDefault(Config_1).default; } });
21
+ var FinalPattern_1 = require("./FinalPattern");
22
+ Object.defineProperty(exports, "FinalPattern", { enumerable: true, get: function () { return __importDefault(FinalPattern_1).default; } });
21
23
  var HorizontalPattern_1 = require("./HorizontalPattern");
22
24
  Object.defineProperty(exports, "HorizontalPattern", { enumerable: true, get: function () { return __importDefault(HorizontalPattern_1).default; } });
23
25
  var isObject_1 = require("./isObject");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scrabble-solver/types",
3
- "version": "2.8.7",
3
+ "version": "2.8.9",
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.7"
27
+ "@scrabble-solver/constants": "^2.8.9"
28
28
  },
29
- "gitHead": "879bf9b68f27fa3d530d0582fd8fbbcd5c8b150b"
29
+ "gitHead": "8557cc2c5214e6689a5c59373b228b28f5dd8ed4"
30
30
  }
package/src/Board.ts CHANGED
@@ -106,6 +106,28 @@ class Board {
106
106
  }, 0);
107
107
  }
108
108
 
109
+ public getWords(): string[] {
110
+ const columns: Cell[][] = [];
111
+
112
+ for (let x = 0; x < this.columnsCount; ++x) {
113
+ const column: Cell[] = [];
114
+
115
+ for (let y = 0; y < this.rowsCount; ++y) {
116
+ column.push(this.rows[y][x]);
117
+ }
118
+
119
+ columns.push(column);
120
+ }
121
+
122
+ const columnsBoard = new Board({ rows: columns });
123
+ const lines = this.toString().split('\n').concat(columnsBoard.toString().split('\n'));
124
+ const words = lines
125
+ .flatMap((line) => line.replaceAll(/\s+/g, EMPTY_CELL).split(' '))
126
+ .filter((word) => word.length > 1);
127
+
128
+ return words;
129
+ }
130
+
109
131
  public isEmpty(): boolean {
110
132
  return this.rows.every((row) => row.every(({ isEmpty }) => isEmpty));
111
133
  }
@@ -115,7 +137,7 @@ class Board {
115
137
  }
116
138
 
117
139
  public toString(): string {
118
- return this.rows.map((row) => row.map(String)).join('\n');
140
+ return this.rows.map((row) => row.map(String).join('')).join('\n');
119
141
  }
120
142
 
121
143
  public updateCell(x: number, y: number, updateCell: (cell: Cell) => Cell): void {
package/src/Config.ts CHANGED
@@ -91,7 +91,7 @@ class Config {
91
91
  }
92
92
 
93
93
  public hasCharacter(character: string): boolean {
94
- return this.alphabet.includes(character);
94
+ return character in this.pointsMap;
95
95
  }
96
96
 
97
97
  public isTwoCharacterTilePrefix(character: string): boolean {
@@ -0,0 +1,16 @@
1
+ import Pattern from './Pattern';
2
+
3
+ class FinalPattern extends Pattern {
4
+ private readonly collisions: Pattern[];
5
+
6
+ constructor(pattern: Pattern) {
7
+ super(pattern.board, pattern.cells);
8
+ this.collisions = pattern.getCollisions();
9
+ }
10
+
11
+ public getCollisions(): Pattern[] {
12
+ return this.collisions;
13
+ }
14
+ }
15
+
16
+ export default FinalPattern;
@@ -2,10 +2,10 @@ import Pattern from './Pattern';
2
2
 
3
3
  class HorizontalPattern extends Pattern {
4
4
  public clone(): Pattern {
5
- return new HorizontalPattern({
6
- board: this.board,
7
- cells: this.cells.map((cell) => cell.clone()),
8
- });
5
+ return new HorizontalPattern(
6
+ this.board,
7
+ this.cells.map((cell) => cell.clone()),
8
+ );
9
9
  }
10
10
 
11
11
  public getCollisions(): Pattern[] {
@@ -27,7 +27,7 @@ class HorizontalPattern extends Pattern {
27
27
  const nextCells = column.slice(cell.y + 1, y);
28
28
  const cells = [...previousCells, cell, ...nextCells];
29
29
  if (cells.length > 1) {
30
- const pattern = new Pattern({ board: this.board, cells });
30
+ const pattern = new Pattern(this.board, cells);
31
31
  collisions.push(pattern);
32
32
  }
33
33
  });
package/src/Pattern.ts CHANGED
@@ -8,7 +8,7 @@ class Pattern {
8
8
 
9
9
  public readonly cells: Cell[];
10
10
 
11
- constructor({ board, cells }: { board: Board; cells: Cell[] }) {
11
+ constructor(board: Board, cells: Cell[]) {
12
12
  this.board = board;
13
13
  this.cells = cells;
14
14
  }
@@ -23,10 +23,10 @@ class Pattern {
23
23
  }
24
24
 
25
25
  public clone(): Pattern {
26
- return new Pattern({
27
- board: this.board,
28
- cells: this.cells.map((cell) => cell.clone()),
29
- });
26
+ return new Pattern(
27
+ this.board,
28
+ this.cells.map((cell) => cell.clone()),
29
+ );
30
30
  }
31
31
 
32
32
  public collides(): boolean {
package/src/Result.ts CHANGED
@@ -103,12 +103,10 @@ const getTiles = (cells: Cell[]): Tile[] => cells.filter(({ isEmpty }) => isEmpt
103
103
 
104
104
  const getTilesCharacters = (tiles: Tile[]): string => getNonBlankCharacters(tiles).sort(charactersComparator).join('');
105
105
 
106
- const getWord = (cells: Cell[]): string => cells.map(String).join('');
106
+ // eslint-disable-next-line prefer-template
107
+ const getWord = (cells: Cell[]): string => cells.reduce((word, cell) => word + cell.toString(), '');
107
108
 
108
- const getWords = (cells: Cell[], collisions: Collision[]): string[] => [
109
- getWord(cells),
110
- ...collisions.map((collision) => collision.map(String).join('')),
111
- ];
109
+ const getWords = (cells: Cell[], collisions: Collision[]): string[] => [cells, ...collisions].map(getWord);
112
110
 
113
111
  const isConsonant = ({ character, isBlank }: Tile): boolean => CONSONANTS.includes(character) && !isBlank;
114
112
 
package/src/Tile.ts CHANGED
@@ -23,9 +23,9 @@ class Tile {
23
23
  toString: () => EMPTY_CELL,
24
24
  });
25
25
 
26
- public readonly character: string;
26
+ public character: string;
27
27
 
28
- public readonly isBlank: boolean;
28
+ public isBlank: boolean;
29
29
 
30
30
  constructor({ character, isBlank = false }: { character: string; isBlank?: boolean }) {
31
31
  this.character = character;
@@ -2,10 +2,10 @@ import Pattern from './Pattern';
2
2
 
3
3
  class VerticalPattern extends Pattern {
4
4
  public clone(): Pattern {
5
- return new VerticalPattern({
6
- board: this.board,
7
- cells: this.cells.map((cell) => cell.clone()),
8
- });
5
+ return new VerticalPattern(
6
+ this.board,
7
+ this.cells.map((cell) => cell.clone()),
8
+ );
9
9
  }
10
10
 
11
11
  public getCollisions(): Pattern[] {
@@ -27,7 +27,7 @@ class VerticalPattern extends Pattern {
27
27
  const nextCells = row.slice(cell.x + 1, x);
28
28
  const cells = [...previousCells, cell, ...nextCells];
29
29
  if (cells.length > 1) {
30
- const pattern = new Pattern({ board: this.board, cells });
30
+ const pattern = new Pattern(this.board, cells);
31
31
  collisions.push(pattern);
32
32
  }
33
33
  });
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export { default as Collision } from './Collision';
10
10
  export { default as CollisionJson } from './CollisionJson';
11
11
  export { default as Config } from './Config';
12
12
  export { default as ConfigJson } from './ConfigJson';
13
+ export { default as FinalPattern } from './FinalPattern';
13
14
  export { default as HorizontalPattern } from './HorizontalPattern';
14
15
  export { default as isObject } from './isObject';
15
16
  export { default as Locale, isLocale } from './Locale';