Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: create website #49

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
.docusaurus
8 changes: 6 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
"plugins": ["@typescript-eslint"],
"rules": {
"prefer-const": "warn",
"no-var": "error"
"no-var": "error",
"no-use-before-define": "off"
},
"overrides": [
{
"files": ["test/*"],
"globals": {
"describe": true,
"it": true
},
}
},
{
"files": ["*.js"],
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ jobs:
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
cache: yarn
- name: Install dependencies
run: npm install
run: yarn
- name: Build package
run: npm run build
run: yarn workspace reading-time build
- name: Test
run: npm test
run: yarn workspace reading-time test
64 changes: 0 additions & 64 deletions .jshintrc

This file was deleted.

1 change: 0 additions & 1 deletion .npmignore → lib/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
src/
test/
tsconfig.json
tsconfig-build.json
29 changes: 29 additions & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "reading-time",
"version": "2.0.0-1",
"description": "Medium's like reading time estimation.",
"main": "dist/index.js",
"browser": "dist/reading-time.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --cache \"**/*.{js,ts}\"",
"spec": "ts-mocha test/*.ts",
"test": "npm run lint && npm run spec",
"build": "tsc",
"prepublishOnly": "npm run build"
},
"repository": "ngryman/reading-time",
"keywords": [
"read",
"time",
"read time",
"reading time",
"medium",
"words per minute"
],
"author": "Nicolas Gryman <[email protected]> (http://ngryman.sh)",
"contributors": [
"Joshua Chen <[email protected]> (https://joshcena.com)"
],
"license": "MIT"
}
File renamed without changes.
61 changes: 28 additions & 33 deletions src/reading-time.ts → lib/src/reading-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type { Options, ReadingTimeStats, WordCountStats, ReadingTimeResult } fro
type WordBoundFunction = Options['wordBound']

function codeIsInRanges(number: number, arrayOfRanges: number[][]) {
return arrayOfRanges.some(([lowerBound, upperBound]) =>
(lowerBound <= number) && (number <= upperBound)
return arrayOfRanges.some(
([lowerBound, upperBound]) => lowerBound <= number && number <= upperBound
)
}

Expand All @@ -20,21 +20,18 @@ const isCJK: WordBoundFunction = (c) => {
// This should be good for most cases, but if you find it unsatisfactory
// (e.g. some other language where each character should be standalone words),
// contributions welcome!
return codeIsInRanges(
charCode,
[
// Hiragana (Katakana not included on purpose,
// context: https://github.com/ngryman/reading-time/pull/35#issuecomment-853364526)
// If you think Katakana should be included and have solid reasons, improvement is welcomed
[0x3040, 0x309f],
// CJK Unified ideographs
[0x4e00, 0x9fff],
// Hangul
[0xac00, 0xd7a3],
// CJK extensions
[0x20000, 0x2ebe0]
]
)
return codeIsInRanges(charCode, [
// Hiragana (Katakana not included on purpose,
// context: https://github.com/ngryman/reading-time/pull/35#issuecomment-853364526)
// If you think Katakana should be included and have solid reasons, improvement is welcomed
[0x3040, 0x309f],
// CJK Unified ideographs
[0x4e00, 0x9fff],
// Hangul
[0xac00, 0xd7a3],
// CJK extensions
[0x20000, 0x2ebe0]
])
}

const isAnsiWordBound: WordBoundFunction = (c) => {
Expand All @@ -43,23 +40,22 @@ const isAnsiWordBound: WordBoundFunction = (c) => {

const isPunctuation: WordBoundFunction = (c) => {
const charCode = c.charCodeAt(0)
return codeIsInRanges(
charCode,
[
[0x21, 0x2f],
[0x3a, 0x40],
[0x5b, 0x60],
[0x7b, 0x7e],
// CJK Symbols and Punctuation
[0x3000, 0x303f],
// Full-width ASCII punctuation variants
[0xff00, 0xffef]
]
)
return codeIsInRanges(charCode, [
[0x21, 0x2f],
[0x3a, 0x40],
[0x5b, 0x60],
[0x7b, 0x7e],
// CJK Symbols and Punctuation
[0x3000, 0x303f],
// Full-width ASCII punctuation variants
[0xff00, 0xffef]
])
}

export function countWords(text: string, options: Options = {}): WordCountStats {
let words = 0, start = 0, end = text.length - 1
let words = 0
, start = 0
, end = text.length - 1
const { wordBound: isWordBound = isAnsiWordBound } = options

// fetch bounds
Expand All @@ -76,8 +72,7 @@ export function countWords(text: string, options: Options = {}): WordCountStats
if (
isCJK(normalizedText[i]) ||
(!isWordBound(normalizedText[i]) &&
(isWordBound(normalizedText[i + 1]) || isCJK(normalizedText[i + 1]))
)
(isWordBound(normalizedText[i + 1]) || isCJK(normalizedText[i + 1])))
) {
words++
}
Expand Down
4 changes: 2 additions & 2 deletions src/stream.ts → lib/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { Transform, TransformCallback } from 'stream'
import type { Options, WordCountStats } from './types'

class ReadingTimeStream extends Transform {
options: Options;
stats: WordCountStats;
options: Options
stats: WordCountStats

constructor(options: Options = {}) {
super({ objectMode: true })
Expand Down
12 changes: 6 additions & 6 deletions src/types.ts → lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
*/

export type Options = {
wordBound?: (char: string) => boolean;
wordsPerMinute?: number;
wordBound?: (char: string) => boolean
wordsPerMinute?: number
}

export type ReadingTimeStats = {
time: number;
minutes: number;
time: number
minutes: number
}

export type WordCountStats = {
total: number;
total: number
}

export type ReadingTimeResult = ReadingTimeStats & {
words: WordCountStats;
words: WordCountStats
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tsconfig-build.json → lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"rootDir": "src",
"declaration": true,
"esModuleInterop": true
},
}
}
Loading