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

Remove mask function #280

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 6 additions & 10 deletions chess.js
Original file line number Diff line number Diff line change
Expand Up @@ -1607,18 +1607,14 @@ export const Chess = function (fen) {
? options.sloppy
: false

function mask(str) {
return str.replace(/\\/g, '\\')
}

function parse_pgn_header(header, options) {
var newline_char =
typeof options === 'object' &&
typeof options.newline_char === 'string'
? options.newline_char
: '\r?\n'
var header_obj = {}
var headers = header.split(new RegExp(mask(newline_char)))
var headers = header.split(new RegExp(newline_char))
var key = ''
var value = ''

Expand Down Expand Up @@ -1647,10 +1643,10 @@ export const Chess = function (fen) {
// With default newline_char, will equal: /^(\[((?:\r?\n)|.)*\])(?:\s*\r?\n){2}/
var header_regex = new RegExp(
'^(\\[((?:' +
mask(newline_char) +
newline_char +
')|.)*\\])' +
'(?:\\s*' +
mask(newline_char) +
newline_char +
'){2}'
)

Expand Down Expand Up @@ -1722,7 +1718,7 @@ export const Chess = function (fen) {
}

var encode_comment = function (string) {
string = string.replace(new RegExp(mask(newline_char), 'g'), ' ')
string = string.replace(new RegExp(newline_char, 'g'), ' ')
return `{${to_hex(string.slice(1, string.length - 1))}}`
}

Expand All @@ -1737,14 +1733,14 @@ export const Chess = function (fen) {
.replace(header_string, '')
.replace(
/* encode comments so they don't get deleted below */
new RegExp(`(\{[^}]*\})+?|;([^${mask(newline_char)}]*)`, 'g'),
new RegExp(`(\{[^}]*\})+?|;([^${newline_char}]*)`, 'g'),
function (match, bracket, semicolon) {
return bracket !== undefined
? encode_comment(bracket)
: ' ' + encode_comment(`{${semicolon.slice(1)}}`)
}
)
.replace(new RegExp(mask(newline_char), 'g'), ' ')
.replace(new RegExp(newline_char, 'g'), ' ')

/* delete recursive annotation variations */
var rav_regex = /(\([^\(\)]+\))+?/g
Expand Down