lodash parseInt
import assert from "assert";
import lodashStable from "lodash";
import { lodashBizarro, whitespace, stubZero } from "./utils.js";
import parseInt from "../parseInt.js";

describe("parseInt", function() {
  it("should accept a `radix`", function() {
    var expected = lodashStable.range(2, 37);

    var actual = lodashStable.map(expected, function(radix) {
      return parseInt("10", radix);
    });

    assert.deepStrictEqual(actual, expected);
  });

  it("should use a radix of `10`, for non-hexadecimals, if `radix` is `undefined` or `0`", function() {
    assert.strictEqual(parseInt("10"), 10);
    assert.strictEqual(parseInt("10", 0), 10);
    assert.strictEqual(parseInt("10", 10), 10);
    assert.strictEqual(parseInt("10", undefined), 10);
  });

  it("should use a radix of `16`, for hexadecimals, if `radix` is `undefined` or `0`", function() {
    lodashStable.each(["0x20", "0X20"], function(string) {
      assert.strictEqual(parseInt(string), 32);
      assert.strictEqual(parseInt(string, 0), 32);
      assert.strictEqual(parseInt(string, 16), 32);
      assert.strictEqual(parseInt(string, undefined), 32);
    });
  });

  it("should use a radix of `10` for string with leading zeros", function() {
    assert.strictEqual(parseInt("08"), 8);
    assert.strictEqual(parseInt("08", 10), 8);
  });

  it("should parse strings with leading whitespace", function() {
    var expected = [8, 8, 10, 10, 32, 32, 32, 32];

    lodashStable.times(2, function(index) {
      var actual = [],
          func = (index ? (lodashBizarro || {}) : _).parseInt;

      if (func) {
        lodashStable.times(2, function(otherIndex) {
          var string = otherIndex ? "10" : "08";
          actual.push(
            func(whitespace + string, 10),
            func(whitespace + string)
          );
        });

        lodashStable.each(["0x20", "0X20"], function(string) {
          actual.push(
            func(whitespace + string),
            func(whitespace + string, 16)
          );
        });

        assert.deepStrictEqual(actual, expected);
      }
    });
  });

  it("should coerce `radix` to a number", function() {
    var object = { "valueOf": stubZero };
    assert.strictEqual(parseInt("08", object), 8);
    assert.strictEqual(parseInt("0x20", object), 32);
  });

  it("should work as an iteratee for methods like `_.map`", function() {
    var strings = lodashStable.map(["6", "08", "10"], Object),
        actual = lodashStable.map(strings, parseInt);

    assert.deepStrictEqual(actual, [6, 8, 10]);

    actual = lodashStable.map("123", parseInt);
    assert.deepStrictEqual(actual, [1, 2, 3]);
  });
});

Related articles

lodash take

import assert from "assert"; import lodashStable from "lodash"; import { falsey, LARGE_ARRAY_SIZE, isEven } from "./utils.js"; import take from "../take.js"; describe("take", function() { var array = [1, 2, 3]; it("should take the first two element

lodash isMatchWith

import assert from "assert"; import lodashStable from "lodash"; import { slice, noop, stubA, falsey, stubFalse, isNpm, mapCaches } from "./utils.js"; import isMatchWith from "../isMatchWith.js"; import isString from "../isString.js"; import last from "..

lodash lodash plant

import assert from "assert";import lodashStable from "lodash";import { square, isNpm } from "./utils.js";import compact from "../compact.js";describe("lodash(...).plant", function() { it("should clone the chained sequence planting `value` as the w

lodash forIn methods

import assert from "assert"; import lodashStable from "lodash"; import { _ } from "./utils.js"; describe("forIn methods", function() { lodashStable.each(["forIn", "forInRight"], function(methodName) { var func = _[methodName]; it("`_." + meth

lodash uniqueId

import assert from "assert";import lodashStable from "lodash";import uniqueId from "../uniqueId.js";describe("uniqueId", function() { it("should generate unique ids", function() { var actual = lodashStable.times(1000, function() { return

lodash toLength

import assert from "assert";import { MAX_INTEGER, MAX_ARRAY_LENGTH } from "./utils.js";import toLength from "../toLength.js";describe("toLength", function() { it("should return a valid length", function() { assert.strictEqual(toLength(-1), 0);

lodash toUpper

import assert from "assert"; import toUpper from "../toUpper.js"; describe("toUpper", function() { it("should convert whole string to upper case", function() { assert.deepStrictEqual(toUpper("--Foo-Bar"), "--FOO-BAR"); assert.deepStrictEqual(t

lodash omit methods

import assert from "assert"; import lodashStable from "lodash"; import { _, symbol, defineProperty } from "./utils.js"; describe("omit methods", function() { lodashStable.each(["omit", "omitBy"], function(methodName) { var expected = { "b": 2, "d"

lodash Strings category methods

import assert from "assert";import lodashStable from "lodash";import { stubString } from "./utils.js";import camelCase from "../camelCase.js";import capitalize from "../capitalize.js";import escape from "../escape.js";import kebabCase from "../keb

lodash partialRight

import assert from "assert"; import lodashStable from "lodash"; import partialRight from "../partialRight.js"; import mergeWith from "../mergeWith.js"; describe("partialRight", function() { it("should work as a deep `_.defaults`", function() { var