xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js map string trim bug All In One

js map string trim bug All In One

js map string trim bug

https://leetcode-cn.com/problems/valid-phone-numbers/

# Read from the file file.txt and output all valid phone numbers to stdout.
# regex
grep -P '^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$' file.txt

https://regexper.com/#%2F^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}%24%2F


const s = `
    987-123-4567
    123 456 7890
    (123) 456-7890
`;
// const arr = s.split('\n').map(String.trim).filter(item => item.length);
// ❌ Uncaught TypeError: undefined is not a function

const arr = s.split('\n').map(String().trim).filter(item => item.length);
// ❌ Uncaught TypeError: String.prototype.trim called on null or undefined

const arr = s.split('\n').map(''.trim).filter(item => item.length);
// ❌ Uncaught TypeError: String.prototype.trim called on null or undefined


const regex = /^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/;

const arr = s.split('\n').map(str => str.trim()).filter(item => item.length && item.match(regex));
// ✅  (2) ['987-123-4567', '(123) 456-7890']

String.trim
// undefined


String;
// ƒ String() { [native code] }
String();
// ''
String().trim;
// ƒ trim() { [native code] }

''.trim
// ƒ trim() { [native code] }
const trim = ''.trim;
ƒ//  trim() { [native code] }
trim.__proto__;
// ƒ () { [native code] }
trim.__proto__.constructor;
// ƒ Function() { [native code] }

trim

const nums = `
 111 ,
 222 ,
 333,
`;

nums.split(',').map(item => item.trim()).filter(str => str.length);
// (3) ['111', '222', '333']

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

js string to number

https://replit.com/@xgqfrms/js-string-to-number#index.js


"use strict";

/**
 * 
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-12-10
 * @modified 
 * 
 * @description 手动实现 js parseInt() 方法
 * @augments 
 * @example 
 * @link https://repl.it/@xgqfrms/js-string-to-number
 * 
 */
  
const log = console.log;

const stringToInt = (str = ``) => {
    let result = 0;
    let negative = false;
    let isExistUnNumber = false;
    let index = 0;
    if(str) {
        negative = str[0] === "-" ? true : false;
        if(negative) {
            str = str.slice(1);
        }
        if(!Number.isInteger(str[0] * 1)) {
            return NaN;
        }
        // for(let item of str.split(``)) {
        //     if(!Number.isInteger(item * 1)) {
        //         isExistUnNumber = true;
        //         index = str.split(``).indexOf(item);
        //         break;
        //     }
        // };
        for(let [i, item] of [...str.split(``)].entries()) {
            // log(item, i);
            if(!Number.isInteger(item * 1)) {
                isExistUnNumber = true;
                index = i;
                break;
            }
        };
        const arr  = isExistUnNumber ? str.split(``).slice(0, index) : str.split(``);
        const len = arr.length;
        arr.forEach((item , i) => {
            result += 10 ** (len - i - 1) * item;
        });
    }
    return negative ? -result : result;
};

// testing

log(stringToInt(`2019`));
// 2019
log(stringToInt(`x2048`));
// NaN
log(stringToInt(`-2048`));
// -2048 
log(stringToInt(`-20-19x`));
// -20

log(`\n`);

log(parseInt(`2019`));
// 2019
log(parseInt(`x2048`));
// NaN
log(parseInt(`-2048`));
// -2048 
log(parseInt(`-20-19x`));
// -20



refs

js map number bug

https://www.cnblogs.com/xgqfrms/p/14313582.html

js string number

https://www.cnblogs.com/xgqfrms/p/10535717.html



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-03-16 23:15  xgqfrms  阅读(81)  评论(0编辑  收藏  举报