@scrabble-solver/types 2.15.11 → 2.15.12

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
@@ -1,4 +1,4 @@
1
- import { BoardJson } from './BoardJson';
1
+ import { type BoardJson } from './BoardJson';
2
2
  import { Cell } from './Cell';
3
3
  export declare class Board {
4
4
  static create: (width: number, height: number) => Board;
package/build/Board.js CHANGED
@@ -70,20 +70,9 @@ class Board {
70
70
  }, 0);
71
71
  }
72
72
  getWords() {
73
- const columns = [];
74
- for (let x = 0; x < this.columnsCount; ++x) {
75
- const column = [];
76
- for (let y = 0; y < this.rowsCount; ++y) {
77
- column.push(this.rows[y][x]);
78
- }
79
- columns.push(column);
80
- }
81
- const columnsBoard = new Board({ rows: columns });
82
- const lines = this.toString().split('\n').concat(columnsBoard.toString().split('\n'));
83
- const words = lines
84
- .flatMap((line) => line.replaceAll(/\s+/g, constants_1.EMPTY_CELL).split(' '))
85
- .filter((word) => word.length > 1);
86
- return words;
73
+ const horizontalWords = getHorizontalWords(this.rows);
74
+ const verticalWords = getHorizontalWords(transpose(this.rows));
75
+ return [...horizontalWords, ...verticalWords];
87
76
  }
88
77
  isEmpty() {
89
78
  return this.rows.every((row) => row.every(({ isEmpty }) => isEmpty));
@@ -113,3 +102,40 @@ Board.fromJson = (json) => {
113
102
  rows: json.map((row) => row.map(Cell_1.Cell.fromJson)),
114
103
  });
115
104
  };
105
+ const transpose = (array) => {
106
+ const rows = array.length;
107
+ const cols = array[0].length;
108
+ const transposed = Array(cols)
109
+ .fill(null)
110
+ .map(() => Array(rows));
111
+ for (let y = 0; y < rows; ++y) {
112
+ for (let x = 0; x < cols; ++x) {
113
+ transposed[x][y] = array[y][x];
114
+ }
115
+ }
116
+ return transposed;
117
+ };
118
+ const getHorizontalWords = (cells) => {
119
+ const words = [];
120
+ for (const row of cells) {
121
+ let currentWord = [];
122
+ for (const cell of row) {
123
+ if (!cell.isEmpty) {
124
+ currentWord.push(cell);
125
+ }
126
+ else if (currentWord.length > 0) {
127
+ if (currentWord.length > 1) {
128
+ words.push(wordToString(currentWord));
129
+ }
130
+ currentWord = [];
131
+ }
132
+ }
133
+ if (currentWord.length > 1) {
134
+ words.push(wordToString(currentWord));
135
+ }
136
+ }
137
+ return words;
138
+ };
139
+ const wordToString = (currentWord) => {
140
+ return currentWord.map((cell) => cell.tile.character).join('');
141
+ };
@@ -1,3 +1,3 @@
1
- import { CellJson } from './CellJson';
1
+ import { type CellJson } from './CellJson';
2
2
  export type BoardJson = CellJson[][];
3
3
  export declare const isBoardJson: (value: unknown) => value is BoardJson;
package/build/Bonus.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { BONUS_CHARACTER, BONUS_WORD } from '@scrabble-solver/constants';
2
- import { BonusJson } from './BonusJson';
3
- import { BonusValue } from './BonusValue';
4
- import { Cell } from './Cell';
1
+ import { type BONUS_CHARACTER, type BONUS_WORD } from '@scrabble-solver/constants';
2
+ import { type BonusJson } from './BonusJson';
3
+ import { type BonusValue } from './BonusValue';
4
+ import { type Cell } from './Cell';
5
5
  import { type Config } from './Config';
6
6
  export declare abstract class Bonus {
7
7
  readonly multiplier: number;
package/build/Cell.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CellJson } from './CellJson';
1
+ import { type CellJson } from './CellJson';
2
2
  import { Tile } from './Tile';
3
3
  export declare class Cell {
4
4
  static fromJson: (json: CellJson) => Cell;
@@ -1,4 +1,4 @@
1
- import { TileJson } from './TileJson';
1
+ import { type TileJson } from './TileJson';
2
2
  export interface CellJson {
3
3
  isEmpty: boolean;
4
4
  tile: TileJson | null;
@@ -1,6 +1,6 @@
1
1
  import { Bonus } from './Bonus';
2
- import { BonusValue } from './BonusValue';
3
- import { Cell } from './Cell';
2
+ import { type BonusValue } from './BonusValue';
3
+ import { type Cell } from './Cell';
4
4
  import { type Config } from './Config';
5
5
  export declare class CharacterBonus extends Bonus {
6
6
  readonly type = "BONUS_CHARACTER";
@@ -1,2 +1,2 @@
1
- import { Cell } from './Cell';
1
+ import { type Cell } from './Cell';
2
2
  export type Collision = Cell[];
@@ -1,2 +1,2 @@
1
- import { CellJson } from './CellJson';
1
+ import { type CellJson } from './CellJson';
2
2
  export type CollisionJson = CellJson[];
package/build/Config.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { Bingo } from './Bingo';
2
- import { Bonus } from './Bonus';
3
- import { BonusValue } from './BonusValue';
4
- import { Cell } from './Cell';
5
- import { ConfigJson } from './ConfigJson';
6
- import { Game } from './Game';
7
- import { Locale } from './Locale';
8
- import { Tile } from './Tile';
9
- import { TileConfig } from './TileConfig';
1
+ import { type Bingo } from './Bingo';
2
+ import { type Bonus } from './Bonus';
3
+ import { type BonusValue } from './BonusValue';
4
+ import { type Cell } from './Cell';
5
+ import { type ConfigJson } from './ConfigJson';
6
+ import { type Game } from './Game';
7
+ import { type Locale } from './Locale';
8
+ import { type Tile } from './Tile';
9
+ import { type TileConfig } from './TileConfig';
10
10
  export declare class Config {
11
11
  static fromJson: (json: ConfigJson) => Config;
12
12
  readonly bonuses: Bonus[];
@@ -1,8 +1,8 @@
1
- import { Bingo } from './Bingo';
2
- import { BonusJson } from './BonusJson';
3
- import { Game } from './Game';
4
- import { Locale } from './Locale';
5
- import { TileConfig } from './TileConfig';
1
+ import { type Bingo } from './Bingo';
2
+ import { type BonusJson } from './BonusJson';
3
+ import { type Game } from './Game';
4
+ import { type Locale } from './Locale';
5
+ import { type TileConfig } from './TileConfig';
6
6
  export interface ConfigJson {
7
7
  bingo: Bingo;
8
8
  blankScore: number;
@@ -1,4 +1,4 @@
1
- import { Locale } from './Locale';
1
+ import { type Locale } from './Locale';
2
2
  export interface Dictionary {
3
3
  description: string;
4
4
  locale: Locale;
@@ -1,7 +1,7 @@
1
- import { Board } from './Board';
2
- import { Cell } from './Cell';
3
- import { Config } from './Config';
4
- import { PatternJson } from './PatternJson';
1
+ import { type Board } from './Board';
2
+ import { type Cell } from './Cell';
3
+ import { type Config } from './Config';
4
+ import { type PatternJson } from './PatternJson';
5
5
  export declare class Pattern {
6
6
  readonly board: Board;
7
7
  readonly cells: Cell[];
@@ -1,4 +1,4 @@
1
- import { CellJson } from './CellJson';
1
+ import { type CellJson } from './CellJson';
2
2
  export interface PatternJson {
3
3
  cells: CellJson[];
4
4
  collisions: PatternJson[];
package/build/Result.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Cell } from './Cell';
2
- import { ResultJson } from './ResultJson';
3
- import { Tile } from './Tile';
2
+ import { type ResultJson } from './ResultJson';
3
+ import { type Tile } from './Tile';
4
4
  type Collision = Cell[];
5
5
  export declare class Result {
6
6
  static fromJson: (json: ResultJson) => Result;
@@ -25,5 +25,7 @@ export declare class Result {
25
25
  points: number;
26
26
  });
27
27
  toJson(): ResultJson;
28
+ isHorizontal(): boolean;
29
+ isVertical(): boolean;
28
30
  }
29
31
  export {};
package/build/Result.js CHANGED
@@ -29,6 +29,12 @@ class Result {
29
29
  points: this.points,
30
30
  };
31
31
  }
32
+ isHorizontal() {
33
+ return this.cells[0].y === this.cells[1].y;
34
+ }
35
+ isVertical() {
36
+ return this.cells[0].x === this.cells[1].x;
37
+ }
32
38
  }
33
39
  exports.Result = Result;
34
40
  Result.fromJson = (json) => {
@@ -1,5 +1,5 @@
1
- import { CellJson } from './CellJson';
2
- import { CollisionJson } from './CollisionJson';
1
+ import { type CellJson } from './CellJson';
2
+ import { type CollisionJson } from './CollisionJson';
3
3
  export interface ResultJson {
4
4
  cells: CellJson[];
5
5
  collisions: CollisionJson[];
package/build/Tile.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TileJson } from './TileJson';
1
+ import { type TileJson } from './TileJson';
2
2
  export declare class Tile {
3
3
  static fromJson: (json: TileJson | null) => Tile;
4
4
  static readonly Null: Tile;
@@ -1,5 +1,5 @@
1
1
  import { Bonus } from './Bonus';
2
- import { BonusValue } from './BonusValue';
2
+ import { type BonusValue } from './BonusValue';
3
3
  export declare class WordBonus extends Bonus {
4
4
  readonly type = "BONUS_WORD";
5
5
  get value(): BonusValue;
@@ -1,4 +1,4 @@
1
- import { WordDefinitionJson } from './WordDefinitionJson';
1
+ import { type WordDefinitionJson } from './WordDefinitionJson';
2
2
  export declare class WordDefinition {
3
3
  static fromJson: (json: WordDefinitionJson | null) => WordDefinition;
4
4
  static readonly Null: WordDefinition;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scrabble-solver/types",
3
- "version": "2.15.11",
3
+ "version": "2.15.12",
4
4
  "description": "Scrabble Solver 2 - Types",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "clean": "rimraf build/"
24
24
  },
25
25
  "dependencies": {
26
- "@scrabble-solver/constants": "^2.15.11"
26
+ "@scrabble-solver/constants": "^2.15.12"
27
27
  },
28
- "gitHead": "4fa527098d5e67120adb7964777c044a09378332"
28
+ "gitHead": "1eeef2b2975e63b195885dd2636c53624d300c6b"
29
29
  }
package/src/Board.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { EMPTY_CELL } from '@scrabble-solver/constants';
2
2
 
3
- import { BoardJson } from './BoardJson';
3
+ import { type BoardJson } from './BoardJson';
4
4
  import { Cell } from './Cell';
5
5
  import { Tile } from './Tile';
6
6
 
@@ -110,25 +110,9 @@ export class Board {
110
110
  }
111
111
 
112
112
  public getWords(): string[] {
113
- const columns: Cell[][] = [];
114
-
115
- for (let x = 0; x < this.columnsCount; ++x) {
116
- const column: Cell[] = [];
117
-
118
- for (let y = 0; y < this.rowsCount; ++y) {
119
- column.push(this.rows[y][x]);
120
- }
121
-
122
- columns.push(column);
123
- }
124
-
125
- const columnsBoard = new Board({ rows: columns });
126
- const lines = this.toString().split('\n').concat(columnsBoard.toString().split('\n'));
127
- const words = lines
128
- .flatMap((line) => line.replaceAll(/\s+/g, EMPTY_CELL).split(' '))
129
- .filter((word) => word.length > 1);
130
-
131
- return words;
113
+ const horizontalWords = getHorizontalWords(this.rows);
114
+ const verticalWords = getHorizontalWords(transpose(this.rows));
115
+ return [...horizontalWords, ...verticalWords];
132
116
  }
133
117
 
134
118
  public isEmpty(): boolean {
@@ -151,3 +135,48 @@ export class Board {
151
135
  this.rows[y] = updateRow(this.rows[y]);
152
136
  }
153
137
  }
138
+
139
+ const transpose = <T>(array: T[][]): T[][] => {
140
+ const rows = array.length;
141
+ const cols = array[0].length;
142
+ const transposed: T[][] = Array(cols)
143
+ .fill(null)
144
+ .map(() => Array(rows));
145
+
146
+ for (let y = 0; y < rows; ++y) {
147
+ for (let x = 0; x < cols; ++x) {
148
+ transposed[x][y] = array[y][x];
149
+ }
150
+ }
151
+
152
+ return transposed;
153
+ };
154
+
155
+ const getHorizontalWords = (cells: Cell[][]): string[] => {
156
+ const words: string[] = [];
157
+
158
+ for (const row of cells) {
159
+ let currentWord: Cell[] = [];
160
+
161
+ for (const cell of row) {
162
+ if (!cell.isEmpty) {
163
+ currentWord.push(cell);
164
+ } else if (currentWord.length > 0) {
165
+ if (currentWord.length > 1) {
166
+ words.push(wordToString(currentWord));
167
+ }
168
+ currentWord = [];
169
+ }
170
+ }
171
+
172
+ if (currentWord.length > 1) {
173
+ words.push(wordToString(currentWord));
174
+ }
175
+ }
176
+
177
+ return words;
178
+ };
179
+
180
+ const wordToString = (currentWord: Cell[]): string => {
181
+ return currentWord.map((cell) => cell.tile.character).join('');
182
+ };
package/src/BoardJson.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CellJson, isCellJson } from './CellJson';
1
+ import { type CellJson, isCellJson } from './CellJson';
2
2
 
3
3
  export type BoardJson = CellJson[][];
4
4
 
package/src/Bonus.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { BONUS_CHARACTER, BONUS_WORD } from '@scrabble-solver/constants';
1
+ import { type BONUS_CHARACTER, type BONUS_WORD } from '@scrabble-solver/constants';
2
2
 
3
- import { BonusJson } from './BonusJson';
4
- import { BonusValue } from './BonusValue';
5
- import { Cell } from './Cell';
3
+ import { type BonusJson } from './BonusJson';
4
+ import { type BonusValue } from './BonusValue';
5
+ import { type Cell } from './Cell';
6
6
  import { type Config } from './Config';
7
7
 
8
8
  export abstract class Bonus {
package/src/Cell.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CellJson } from './CellJson';
1
+ import { type CellJson } from './CellJson';
2
2
  import { Tile } from './Tile';
3
3
 
4
4
  export class Cell {
package/src/CellJson.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isObject } from './isObject';
2
- import { isTileJson, TileJson } from './TileJson';
2
+ import { isTileJson, type TileJson } from './TileJson';
3
3
 
4
4
  export interface CellJson {
5
5
  isEmpty: boolean;
@@ -1,8 +1,8 @@
1
1
  import { BONUS_CHARACTER } from '@scrabble-solver/constants';
2
2
 
3
3
  import { Bonus } from './Bonus';
4
- import { BonusValue } from './BonusValue';
5
- import { Cell } from './Cell';
4
+ import { type BonusValue } from './BonusValue';
5
+ import { type Cell } from './Cell';
6
6
  import { type Config } from './Config';
7
7
 
8
8
  export class CharacterBonus extends Bonus {
package/src/Collision.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { Cell } from './Cell';
1
+ import { type Cell } from './Cell';
2
2
 
3
3
  export type Collision = Cell[];
@@ -1,3 +1,3 @@
1
- import { CellJson } from './CellJson';
1
+ import { type CellJson } from './CellJson';
2
2
 
3
3
  export type CollisionJson = CellJson[];
package/src/Config.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { BLANK, BONUS_CHARACTER, BONUS_WORD, NO_BONUS } from '@scrabble-solver/constants';
2
2
 
3
- import { Bingo } from './Bingo';
4
- import { Bonus } from './Bonus';
5
- import { BonusValue } from './BonusValue';
6
- import { Cell } from './Cell';
3
+ import { type Bingo } from './Bingo';
4
+ import { type Bonus } from './Bonus';
5
+ import { type BonusValue } from './BonusValue';
6
+ import { type Cell } from './Cell';
7
7
  import { CharacterBonus } from './CharacterBonus';
8
- import { ConfigJson } from './ConfigJson';
9
- import { Game } from './Game';
10
- import { Locale } from './Locale';
11
- import { Tile } from './Tile';
12
- import { TileConfig } from './TileConfig';
8
+ import { type ConfigJson } from './ConfigJson';
9
+ import { type Game } from './Game';
10
+ import { type Locale } from './Locale';
11
+ import { type Tile } from './Tile';
12
+ import { type TileConfig } from './TileConfig';
13
13
  import { WordBonus } from './WordBonus';
14
14
 
15
15
  export class Config {
package/src/ConfigJson.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { Bingo } from './Bingo';
2
- import { BonusJson } from './BonusJson';
3
- import { Game } from './Game';
4
- import { Locale } from './Locale';
5
- import { TileConfig } from './TileConfig';
1
+ import { type Bingo } from './Bingo';
2
+ import { type BonusJson } from './BonusJson';
3
+ import { type Game } from './Game';
4
+ import { type Locale } from './Locale';
5
+ import { type TileConfig } from './TileConfig';
6
6
 
7
7
  export interface ConfigJson {
8
8
  bingo: Bingo;
package/src/Dictionary.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Locale } from './Locale';
1
+ import { type Locale } from './Locale';
2
2
 
3
3
  export interface Dictionary {
4
4
  description: string;
package/src/Pattern.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Board } from './Board';
2
- import { Cell } from './Cell';
3
- import { Config } from './Config';
4
- import { PatternJson } from './PatternJson';
1
+ import { type Board } from './Board';
2
+ import { type Cell } from './Cell';
3
+ import { type Config } from './Config';
4
+ import { type PatternJson } from './PatternJson';
5
5
 
6
6
  export class Pattern {
7
7
  public readonly board: Board;
@@ -1,4 +1,4 @@
1
- import { CellJson } from './CellJson';
1
+ import { type CellJson } from './CellJson';
2
2
 
3
3
  export interface PatternJson {
4
4
  cells: CellJson[];
package/src/Result.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { CONSONANTS, VOWELS } from '@scrabble-solver/constants';
2
2
 
3
3
  import { Cell } from './Cell';
4
- import { ResultJson } from './ResultJson';
5
- import { Tile } from './Tile';
4
+ import { type ResultJson } from './ResultJson';
5
+ import { type Tile } from './Tile';
6
6
 
7
7
  type Collision = Cell[];
8
8
 
@@ -80,6 +80,14 @@ export class Result {
80
80
  points: this.points,
81
81
  };
82
82
  }
83
+
84
+ public isHorizontal(): boolean {
85
+ return this.cells[0].y === this.cells[1].y;
86
+ }
87
+
88
+ public isVertical(): boolean {
89
+ return this.cells[0].x === this.cells[1].x;
90
+ }
83
91
  }
84
92
 
85
93
  const getBlanks = (tiles: Tile[]): Tile[] => tiles.filter(({ isBlank }) => isBlank);
package/src/ResultJson.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { CellJson } from './CellJson';
2
- import { CollisionJson } from './CollisionJson';
1
+ import { type CellJson } from './CellJson';
2
+ import { type CollisionJson } from './CollisionJson';
3
3
 
4
4
  export interface ResultJson {
5
5
  cells: CellJson[];
package/src/Tile.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { EMPTY_CELL } from '@scrabble-solver/constants';
2
2
 
3
- import { TileJson } from './TileJson';
3
+ import { type TileJson } from './TileJson';
4
4
 
5
5
  export class Tile {
6
6
  public static fromJson = (json: TileJson | null): Tile => {
package/src/WordBonus.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { BONUS_WORD } from '@scrabble-solver/constants';
2
2
 
3
3
  import { Bonus } from './Bonus';
4
- import { BonusValue } from './BonusValue';
4
+ import { type BonusValue } from './BonusValue';
5
5
 
6
6
  export class WordBonus extends Bonus {
7
7
  public readonly type = BONUS_WORD;
@@ -1,4 +1,4 @@
1
- import { WordDefinitionJson } from './WordDefinitionJson';
1
+ import { type WordDefinitionJson } from './WordDefinitionJson';
2
2
 
3
3
  export class WordDefinition {
4
4
  public static fromJson = (json: WordDefinitionJson | null): WordDefinition => {