function to_map_ref(digits, easting, northing) {
  if (![ 0,2,4,6,8,10,12,14,16 ].includes(Number(digits))) throw new RangeError(`invalid precision '${digits}'`); // eslint-disable-line comma-spacing

  let e = easting;
  let n = northing;

  // use digits = 0 to return numeric format (in metres) - note northing may be >= 1e7
  if (digits == 0) {
    const format = { useGrouping: false,  minimumIntegerDigits: 6, maximumFractionDigits: 3 };
    const ePad = e.toLocaleString('en', format);
    const nPad = n.toLocaleString('en', format);
    return `${ePad},${nPad}`;
  }

  // get the 100km-grid indices
  const e100km = Math.floor(e / 100000), n100km = Math.floor(n / 100000);

  // translate those into numeric equivalents of the grid letters
  let l1 = (19 - n100km) - (19 - n100km) % 5 + Math.floor((e100km + 10) / 5);
  let l2 = (19 - n100km) * 5 % 25 + e100km % 5;

  // compensate for skipped 'I' and build grid letter-pairs
  if (l1 > 7) l1++;
  if (l2 > 7) l2++;
  const letterPair = String.fromCharCode(l1 + 'A'.charCodeAt(0), l2 + 'A'.charCodeAt(0));

  // strip 100km-grid indices from easting & northing, and reduce precision
  e = Math.floor((e % 100000) / Math.pow(10, 5 - digits / 2));
  n = Math.floor((n % 100000) / Math.pow(10, 5 - digits / 2));

  // pad eastings & northings with leading zeros
  e = e.toString().padStart(digits/2, '0');
  n = n.toString().padStart(digits/2, '0');

  return `${letterPair} ${e} ${n}`;
}
/*
 * converts northing, easting to standard OS grid reference.
 *
 * [digits=10] - precision (10 digits = metres)
 *   to_map_ref(8, 651409, 313177); => 'TG 5140 1317'
 *   to_map_ref(0, 651409, 313177); => '651409,313177'
 *
 */
converts northing, easting to standard OS grid reference.
converts northing easting to standard OS grid reference .
function to_map_ref(digits, easting, northing) {
  if (![ 0,2,4,6,8,10,12,14,16 ].includes(Number(digits))) throw new RangeError(`invalid precision '${digits}'`); 
  let e = easting;
  let n = northing;
  if (digits == 0) {
    const format = { useGrouping: false,  minimumIntegerDigits: 6, maximumFractionDigits: 3 };
    const ePad = e.toLocaleString('en', format);
    const nPad = n.toLocaleString('en', format);
    return `${ePad},${nPad}`;
  }
  const e100km = Math.floor(e / 100000), n100km = Math.floor(n / 100000);
  let l1 = (19 - n100km) - (19 - n100km) % 5 + Math.floor((e100km + 10) / 5);
  let l2 = (19 - n100km) * 5 % 25 + e100km % 5;
  if (l1 > 7) l1++;
  if (l2 > 7) l2++;
  const letterPair = String.fromCharCode(l1 + 'A'.charCodeAt(0), l2 + 'A'.charCodeAt(0));
  e = Math.floor((e % 100000) / Math.pow(10, 5 - digits / 2));
  n = Math.floor((n % 100000) / Math.pow(10, 5 - digits / 2));
  e = e.toString().padStart(digits/2, '0');
  n = n.toString().padStart(digits/2, '0');
  return `${letterPair} ${e} ${n}`;
}
function to_map_ref ( digits , easting , northing ) { if ( ! [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ] . includes ( Number ( digits ) ) ) throw new RangeError ( ` ${ digits } ` ) ; let e = easting ; let n = northing ; if ( digits == 0 ) { const format = { useGrouping : false , minimumIntegerDigits : 6 , maximumFractionDigits : 3 } ; const ePad = e . toLocaleString ( 'en' , format ) ; const nPad = n . toLocaleString ( 'en' , format ) ; return ` ${ ePad } ${ nPad } ` ; } const e100km = Math . floor ( e / 100000 ) , n100km = Math . floor ( n / 100000 ) ; let l1 = ( 19 - n100km ) - ( 19 - n100km ) % 5 + Math . floor ( ( e100km + 10 ) / 5 ) ; let l2 = ( 19 - n100km ) * 5 % 25 + e100km % 5 ; if ( l1 > 7 ) l1 ++ ; if ( l2 > 7 ) l2 ++ ; const letterPair = String . fromCharCode ( l1 + 'A' . charCodeAt ( 0 ) , l2 + 'A' . charCodeAt ( 0 ) ) ; e = Math . floor ( ( e % 100000 ) / Math . pow ( 10 , 5 - digits / 2 ) ) ; n = Math . floor ( ( n % 100000 ) / Math . pow ( 10 , 5 - digits / 2 ) ) ; e = e . toString ( ) . padStart ( digits / 2 , '0' ) ; n = n . toString ( ) . padStart ( digits / 2 , '0' ) ; return ` ${ letterPair } ${ e } ${ n } ` ; }
converts northing, easting to standard OS grid reference.
converts northing easting to standard OS grid reference .
// eslint-disable-line comma-spacing // use digits = 0 to return numeric format (in metres) - note northing may be >= 1e7 // get the 100km-grid indices // translate those into numeric equivalents of the grid letters // compensate for skipped 'I' and build grid letter-pairs // strip 100km-grid indices from easting & northing, and reduce precision // pad eastings & northings with leading zeros