Skip to content

Commit 2b2a571

Browse files
committed
Update property names and add performance benchmark tests
1 parent fed5800 commit 2b2a571

File tree

4 files changed

+150
-6
lines changed

4 files changed

+150
-6
lines changed

packages/block-parser/src/index-1.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ const defaultOptions = {
6666
/**
6767
* @typedef {Object} ParseBlocksResult
6868
* @property {RegExp} pattern - The regex pattern used
69-
* @property {RegExp} COMMENT_OPEN_REGEX - Regex for open comments
70-
* @property {RegExp} COMMENT_CLOSE_REGEX - Regex for close comments
69+
* @property {RegExp} openPattern - Regex for open comments
70+
* @property {RegExp} closePattern - Regex for close comments
7171
* @property {BlockData[]} blocks - Array of parsed blocks
7272
*/
7373

@@ -257,10 +257,10 @@ Details:
257257
// Close but no single line newPattern: newGetBlockRegex({ openComment, commentClose, start: START, ending: END }),
258258
// pattern: regexToUse,
259259
pattern: newerRegex,
260-
// COMMENT_OPEN_REGEX: openTagRegex,
261-
// COMMENT_CLOSE_REGEX: closeTagRegex,
262-
COMMENT_OPEN_REGEX: patterns.openPattern,
263-
COMMENT_CLOSE_REGEX: patterns.closePattern,
260+
// openPattern: openTagRegex,
261+
// closePattern: closeTagRegex,
262+
openPattern: patterns.openPattern,
263+
closePattern: patterns.closePattern,
264264
blocks: newBlocks
265265
}
266266
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { performance } = require('perf_hooks')
2+
const { dedentInline } = require('./text.test')
3+
4+
function dedentInlineManual(text) {
5+
if (!text) return { minIndent: 0, text: '' }
6+
const lines = text.split('\n')
7+
let minIndent = null
8+
for (let i = 0; i < lines.length; i++) {
9+
const line = lines[i]
10+
if (line.trim() === '') continue
11+
let indent = 0
12+
while (indent < line.length && (line[indent] === ' ' || line[indent] === '\t')) indent++
13+
if (minIndent === null || indent < minIndent) minIndent = indent
14+
}
15+
if (minIndent === null) return { minIndent: 0, text: text.trim() }
16+
17+
let result = ''
18+
for (let i = 0; i < lines.length; i++) {
19+
const line = lines[i]
20+
let leading = 0
21+
while (leading < line.length && (line[leading] === ' ' || line[leading] === '\t')) leading++
22+
const toRemove = Math.min(leading, minIndent)
23+
result += line.slice(toRemove) + '\n'
24+
}
25+
result = result.slice(0, -1) // Remove trailing newline
26+
const cleanResult = result.replace(/^[\r\n]+|[\r\n]+$/g, '')
27+
return { minIndent, text: cleanResult }
28+
}
29+
30+
function makeTestString(lines = 10000, indent = 4) {
31+
const pad = ' '.repeat(indent)
32+
let out = ''
33+
for (let i = 0; i < lines; i++) {
34+
out += pad + 'line ' + i + '\n'
35+
}
36+
return out
37+
}
38+
39+
const bigString = makeTestString(10000, 6)
40+
41+
console.log('Benchmarking dedentInline (regex)...')
42+
let t0 = performance.now()
43+
for (let i = 0; i < 20; i++) dedentInline(bigString)
44+
let t1 = performance.now()
45+
console.log('dedentInline (regex) avg:', ((t1 - t0) / 20).toFixed(2), 'ms')
46+
47+
console.log('Benchmarking dedentInlineManual (manual)...')
48+
t0 = performance.now()
49+
for (let i = 0; i < 20; i++) dedentInlineManual(bigString)
50+
t1 = performance.now()
51+
console.log('dedentInlineManual (manual) avg:', ((t1 - t0) / 20).toFixed(2), 'ms')
52+
53+
module.exports = { dedentInlineManual }
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const { parseBlocks } = require('../../src')
4+
const { getTextBetweenChars, getBlockText, dedentString } = require('../../src/text')
5+
6+
const util = require('util')
7+
8+
let DEBUG = process.argv.includes('--debug') ? true : false
9+
// DEBUG = true
10+
const logger = DEBUG ? deepLog : () => {}
11+
12+
function logValue(value, isFirst, isLast) {
13+
const prefix = `${isFirst ? '> ' : ''}`
14+
if (typeof value === 'object') {
15+
console.log(`${util.inspect(value, false, null, true)}\n`)
16+
return
17+
}
18+
if (isFirst) {
19+
console.log(`\n\x1b[33m${prefix}${value}\x1b[0m`)
20+
return
21+
}
22+
console.log((typeof value === 'string' && value.includes('\n')) ? `\`${value}\`` : value)
23+
// isLast && console.log(`\x1b[37m\x1b[1m${'─'.repeat(94)}\x1b[0m\n`)
24+
}
25+
26+
function deepLog() {
27+
for (let i = 0; i < arguments.length; i++) logValue(arguments[i], i === 0, i === arguments.length - 1)
28+
}
29+
30+
31+
const yaml = fs.readFileSync(path.join(__dirname, '_yaml.yml'), 'utf8')
32+
33+
const details = parseBlocks(yaml, {
34+
syntax: 'yaml',
35+
open: 'block',
36+
close: '/block',
37+
})
38+
39+
deepLog(details)
40+
41+
const block = details.blocks
42+
console.log('Raw text between open and close tags')
43+
console.log(getTextBetweenChars(yaml, block[0].block.start, block[0].block.end))
44+
45+
console.log('───────────────────────────────')
46+
console.log('Full block text')
47+
const getText = getBlockText(yaml, block[0].block)
48+
console.log(getText)
49+
50+
console.log(getText === block[0].block.match)
51+
52+
console.log('───────────────────────────────')
53+
console.log('Dedented block text')
54+
const dedentText = dedentString(getText)
55+
console.log(dedentText)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test Workflow
2+
description: A simple test workflow for testing purposes
3+
version: 1.0.0
4+
5+
on:
6+
push:
7+
branches: [main, develop]
8+
pull_request:
9+
branches: [main]
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Run tests
19+
run: npm test
20+
21+
## block remove ##
22+
- name: Run tests two
23+
run: npm test two
24+
## /block ##
25+
26+
## block x ##
27+
- name: Run tests two
28+
run: npm test two
29+
## /block ##
30+
31+
build:
32+
runs-on: ubuntu-latest
33+
needs: test
34+
steps:
35+
- name: Build project
36+
run: npm run build

0 commit comments

Comments
 (0)