@blex41/word-search

中文文档
1.2.0发布于 2021/06/16MITCommonJS近 30 天下载 2973

Word search puzzle generator

原始使用文档
文档目录11 节
  1. Installation
  2. Usage
  3. Options
  4. Properties and methods
  5. ws.grid
  6. ws.words
  7. ws.forbiddenWordsIncluded
  8. ws.dump()
  9. ws.load(backup)
  10. ws.toString()
  11. ws.read(start, end)

word-search

Build Status Coverage Status

Word search puzzle generator.

Installation

With NodeJS:

npm install -S @blex41/word-search

... and then:

const WordSearch = require("@blex41/word-search");

In a browser:

<script src="https://unpkg.com/@blex41/word-search@latest/dist/wordsearch.min.js"></script>
<script>
  /* WordSearch is available here */
</script>

Usage

// If an option is missing, it will be given a default value
const options = {
  cols: 6,
  rows: 6,
  disabledDirections: ["N", "W", "NW", "SW"],
  dictionary: ["Hello", "crêpe", "Škoda", "word", "search"],
  maxWords: 20,
  backwardsProbability: 0.3,
  upperCase: true,
  diacritics: true
};

// Create a new puzzle
const ws = new WordSearch(options);

// Use its methods
console.log(ws.toString());
// S Š M Y W X
// E C K M O G
// A R Z O R A
// R Ê N F D I
// C P I Y Q A
// H E L L O I

Options

Here are the options you can pass when creating a new puzzle:

nametypedefaultdescription
colsInteger10Number of columns
rowsInteger10Number of rows
disabledDirectionsArray.String[]Directions to disable (any of "N", "S", "E", "W", "NE", "NW", "SE" or "SW")
dictionaryArray.String[]Words to insert in the grid (some of them may not be inserted if they're too long, cannot be placed, or if maxWords is exceeded)
maxWordsInteger20Maximum number of words to insert
backwardsProbabilityFloat0.3Probability to have each word written backwards (it's a probability, not a strict percentage)
upperCaseBooleantrueWhether the letters in the grid should be uppercase
diacriticsBooleanfalseWhether the letters in the grid should keep their diacritics (accents)
forbiddenWordsArray.String[]Words which should not appear accidentally in the final grid, in any direction (e.g. profanity). Will try rebuilding it for maxRetries
maxRetriesInteger10Used only for the forbiddenWords option - how many attempts should be made to rebuild the grid when finding forbidden words in it

Properties and methods

Once you have created your instance, you can access these Read-Only properties:

ws.grid

Returns the puzzle's grid

[
  ["S", "Š", "M", "Y", "W", "X"],
  ["E", "C", "K", "M", "O", "G"],
  ["A", "R", "Z", "O", "R", "A"],
  ["R", "Ê", "N", "F", "D", "I"],
  ["C", "P", "I", "Y", "Q", "A"],
  ["H", "E", "L", "L", "O", "I"]
]

ws.words

Returns the words that were successfully inserted in the grid:

[
  {
    word: "word",
    clean: "WORD",
    path: [
      { x: 4, y: 0 }, // W
      { x: 4, y: 1 }, // O
      { x: 4, y: 2 }, // R
      { x: 4, y: 3 } // D
    ]
  },
  {
    word: "crêpe",
    clean: "CRÊPE",
    path: [
      { x: 1, y: 1 }, // C
      { x: 1, y: 2 }, // R
      { x: 1, y: 3 }, // Ê
      { x: 1, y: 4 }, // P
      { x: 1, y: 5 } // E
    ]
  }
  /* ... */
];

ws.forbiddenWordsIncluded

Returns an Array of forbidden words which were inserted into the grid because no other solution was found after maxRetries:

["CRAP"]

You can also use these methods:

ws.dump()

Returns an Object representing the state of the puzzle. This can be useful if you want to save a game and reuse it later using ws.load(backup).

ws.load(backup)

Loads a game from an Object of a previous dump.

const ws1 = new WordSearch(/* ... */);
const backup = ws1.dump();
const ws2 = new WordSearch().load(backup);
// ws2 is now a copy of ws1

ws.toString()

Returns a String representing the grid of the puzzle. This can be useful if you want to print it to the console.

S Š M Y W X
E C K M O G
A R Z O R A
R Ê N F D I
C P I Y Q A
H E L L O I

ws.read(start, end)

Reads the letters from start to end positions and returns the String it forms. This can be useful to check what letters a user highlighted.

ws.read({ x: 1, y: 0 }, { x: 5, y: 4 }); // "ŠKODA"

版本与发布记录

@blex41/word-search 共发布 15 个版本,首次发布于 2018/12/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.0latest2021/06/165 年前
1.1.02019/01/187 年前
1.0.112019/01/157 年前
1.0.102019/01/127 年前
1.0.92019/01/127 年前
1.0.82019/01/127 年前

关于 @blex41/word-search 的常见问题

@blex41/word-search 是什么?

Word search puzzle generator

如何安装 @blex41/word-search?

在项目目录运行 npm install @blex41/word-search,或使用 pnpm add / yarn add / bun add。

@blex41/word-search 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/blex41__word-search 是否存在。

@blex41/word-search 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@blex41/word-search 可以商用吗?

@blex41/word-search 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@blex41/word-search 的下载量有多少?

根据 npm 官方下载统计,@blex41/word-search 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,973 次,日均约 99 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@blex41/word-search 还在维护吗?

最新版本 1.2.0 发布于 2021/06/16(5 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/12/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。