parseMove

Benchmark created on


Setup

function error(str) {
	throw str
}

function isPosition() {
	return true
}

function rand() {
    return Math.floor(Math.random() * 91)
}

function pluck(exclude) {
	let current = positions[rand()]
	
	return current !== exclude ? current : pluck(exclude)
}

const positions = [
  'f11',
  'e10',
  'f10',
  'g10',
  'd9',
  'e9',
  'f9',
  'g9',
  'h9',
  'c8',
  'd8',
  'e8',
  'f8',
  'g8',
  'h8',
  'i8',
  'b7',
  'c7',
  'd7',
  'e7',
  'f7',
  'g7',
  'h7',
  'i7',
  'k7',
  'a6',
  'b6',
  'c6',
  'd6',
  'e6',
  'f6',
  'g6',
  'h6',
  'i6',
  'k6',
  'l6',
  'a5',
  'b5',
  'c5',
  'd5',
  'e5',
  'f5',
  'g5',
  'h5',
  'i5',
  'k5',
  'l5',
  'a4',
  'b4',
  'c4',
  'd4',
  'e4',
  'f4',
  'g4',
  'h4',
  'i4',
  'k4',
  'l4',
  'a3',
  'b3',
  'c3',
  'd3',
  'e3',
  'f3',
  'g3',
  'h3',
  'i3',
  'k3',
  'l3',
  'a2',
  'b2',
  'c2',
  'd2',
  'e2',
  'f2',
  'g2',
  'h2',
  'i2',
  'k2',
  'l2',
  'a1',
  'b1',
  'c1',
  'd1',
  'e1',
  'f1',
  'g1',
  'h1',
  'i1',
  'k1',
  'l1',
]

Test runner

Ready to run.

Testing in
TestOps/sec
1.7.2
function parseMove(source) {
  const from = positions.find(position => source.startsWith(position))

  if (!from) {
    error('parse failed: invalid from position')
  }

  let rest = source.slice(from.length)

  const to = positions.find(position => rest.startsWith(position))

  if (!to) {
    error('parse failed: invalid to position')
  }

  if (from === to) {
    error('parse failed: identical from and to positions')
  }

  rest = rest.slice(to.length)

  if (rest) {
    if (
      rest === 'b' ||
      rest === 'n' ||
      rest === 'q' ||
      rest === 'r'
    ) {
      if (
        !promotionPositions['b'].includes(to) &&
        !promotionPositions['w'].includes(to)
      ) {
        error('parse failed: invalid promotion position')
      }

      return { from, to, promotion: rest }
    }

    error('parse failed: invalid promotion')
  }

  return { from, to }
}

const from = pluck()
const to = pluck(from)

parseMove(`${from}${to}`)

ready
test
function parseMove(source) {
  const fromFile = source[0]

  const fromRank = (source[1] === '1' && (source[2] === '0' || source[2] === '1'))
    ? `${source[1]}${source[2]}`
    : source[1]

  const from = `${fromFile}${fromRank}`

  if (!from || !isPosition(from)) {
    error('parse failed: invalid from position')
  }

  const toFile = source[from.length]

  const toRank = (source[from.length + 1] === '1' && (source[from.length + 2] === '0' || source[from.length + 2] === '1'))
    ? `${source[from.length + 1]}${source[from.length + 2]}`
    : source[from.length + 1]

  const to = `${toFile}${toRank}`

  if (!to || !isPosition(to)) {
    error('parse failed: invalid to position')
  }

  if (from === to) {
    error('parse failed: identical from and to positions')
  }

  const promotion = source[from.length + to.length]

  if (promotion) {
    if (
      promotion === 'b' ||
      promotion === 'n' ||
      promotion === 'q' ||
      promotion === 'r'
    ) {
      if (
        !promotionPositions['b'].includes(to) &&
        !promotionPositions['w'].includes(to)
      ) {
        error('parse failed: invalid promotion position')
      }

      return { from, to, promotion }
    }

    error('parse failed: invalid promotion')
  }

  return { from, to }
}

const from = pluck()
const to = pluck(from)

parseMove(`${from}${to}`)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.