time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

remove_utf_8_bom.js (1822B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     24 var format = require( './../../format' );
     25 
     26 
     27 // VARIABLES //
     28 
     29 // '\ufeff' => 1111111011111111 => 0xFEFF => 65279
     30 var BOM = 65279;
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Removes a UTF-8 byte order mark (BOM) from the beginning of a string.
     37 *
     38 * ## Notes
     39 *
     40 * -   A UTF-8 byte order mark ([BOM][1]) is the byte sequence `0xEF,0xBB,0xBF`.
     41 * -   To convert a UTF-8 encoded `Buffer` to a `string`, the `Buffer` must be converted to [UTF-16][2]. The BOM thus gets converted to the single 16-bit code point `'\ufeff'` (UTF-16 BOM).
     42 *
     43 * [1]: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
     44 * [2]: http://es5.github.io/#x4.3.16
     45 *
     46 *
     47 * @param {string} str - input string
     48 * @throws {TypeError} must provide a string primitive
     49 * @returns {string} string with BOM removed
     50 *
     51 * @example
     52 * var str = removeUTF8BOM( '\ufeffbeep' );
     53 * // returns 'beep'
     54 */
     55 function removeUTF8BOM( str ) {
     56 	if ( !isString( str ) ) {
     57 		throw new TypeError( format( 'invalid argument. Must provide a string. Value: `%s`.', str ) );
     58 	}
     59 	if ( str.charCodeAt( 0 ) === BOM ) {
     60 		return str.slice( 1 );
     61 	}
     62 	return str;
     63 }
     64 
     65 
     66 // EXPORTS //
     67 
     68 module.exports = removeUTF8BOM;