dev-sidecar/packages/mitmproxy/test/matchTest.js

31 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-02-08 08:25:13 +00:00
const assert = require('node:assert')
const name = '/docmirror/dev-sidecar/raw/master/doc/index.png'
// https://raw.fastgit.org/docmirror/dev-sidecar/master/doc/index.png
const ret = name.replace(/^(.+)\/raw\/(.+)$/, 'raw.fastgit.org$1/$2')
console.log(ret)
2025-02-08 08:25:13 +00:00
assert.strictEqual(ret, 'raw.fastgit.org/docmirror/dev-sidecar/master/doc/index.png')
2020-11-17 10:49:46 +00:00
2025-02-08 08:25:13 +00:00
const reg = /^\/[^/]+\/[^/]+$/
2020-11-17 10:49:46 +00:00
console.log('/greper/d2-crud-plus/blob/master/.eslintignore'.match(reg))
2025-02-08 08:25:13 +00:00
assert.strictEqual('/greper/d2-crud-plus/blob/master/.eslintignore'.match(reg), null)
const chunk = Buffer.from('<head></head>')
const script = '<script>a</script>'
const index = chunk.indexOf('</head>')
const scriptBuf = Buffer.from(script)
const chunkNew = Buffer.alloc(chunk.length + scriptBuf.length)
chunk.copy(chunkNew, 0, 0, index)
scriptBuf.copy(chunkNew, index, 0)
chunk.copy(chunkNew, index + scriptBuf.length, index)
console.log(chunkNew.toString())
2025-02-08 08:25:13 +00:00
assert.strictEqual(chunkNew.toString(), '<head><script>a</script></head>')
const reg2 = /aaaa/i
console.log(reg2.test('aaaa')) // true
assert.strictEqual(reg2.test('aaaa'), true)
2020-12-02 04:21:49 +00:00
2025-02-08 08:25:13 +00:00
const reg3 = '/aaaa/i'
console.log(new RegExp(reg3).test('aaaa')) // false
assert.strictEqual(new RegExp(reg3).test('aaaa'), false)