@alduino/humanizer
Advanced tools
| export * from "./dist/dateTime"; | ||
| export {}; |
| module.exports = require("./dist/dateTime"); |
| import IDateTimeHumanizeStrategy from "./IDateTimeHumanizeStrategy"; | ||
| export default class DefaultDateTimeHumanizeStrategy implements IDateTimeHumanizeStrategy { | ||
| humanize(input: Date, comparisonBase: Date): string; | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports["default"] = void 0; | ||
| var _inflections = require("../string/inflections"); | ||
| var _number = require("../number"); | ||
| function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
| function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
| function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
| var TimeUnit; | ||
| (function (TimeUnit) { | ||
| TimeUnit["Year"] = "years"; | ||
| TimeUnit["Month"] = "months"; | ||
| TimeUnit["Day"] = "days"; | ||
| TimeUnit["Hour"] = "hours"; | ||
| TimeUnit["Minute"] = "minutes"; | ||
| TimeUnit["Second"] = "seconds"; | ||
| TimeUnit["Millisecond"] = "milliseconds"; | ||
| })(TimeUnit || (TimeUnit = {})); | ||
| var Tense; | ||
| (function (Tense) { | ||
| Tense[Tense["Future"] = 0] = "Future"; | ||
| Tense[Tense["Past"] = 1] = "Past"; | ||
| })(Tense || (Tense = {})); | ||
| function humanise(unit, tense, amount) { | ||
| if (amount === 0) return "now"; | ||
| var amountStr = amount < 10 ? (0, _number.words)(amount) : amount.toString(); | ||
| var value = "".concat(amountStr, " ").concat(amount === 1 ? (0, _inflections.singularize)(unit) : unit); | ||
| if (tense === Tense.Future) return "in ".concat(value); | ||
| return "".concat(value, " ago"); | ||
| } | ||
| var DefaultDateTimeHumanizeStrategy = /*#__PURE__*/function () { | ||
| function DefaultDateTimeHumanizeStrategy() { | ||
| _classCallCheck(this, DefaultDateTimeHumanizeStrategy); | ||
| } | ||
| _createClass(DefaultDateTimeHumanizeStrategy, [{ | ||
| key: "humanize", | ||
| value: function humanize(input, comparisonBase) { | ||
| var difference = Math.abs(comparisonBase.getTime() - input.getTime()); | ||
| var tense = input.getTime() > comparisonBase.getTime() ? Tense.Future : Tense.Past; | ||
| var milliseconds = difference % 1000, | ||
| seconds = Math.floor(difference / 1000) % 60, | ||
| minutes = Math.floor(difference / 1000 / 60) % 60, | ||
| hours = Math.floor(difference / 1000 / 60 / 60) % 24, | ||
| days = Math.floor(difference / 1000 / 60 / 60 / 24); | ||
| var years = 0, | ||
| months = 0; | ||
| if (milliseconds >= 1000) seconds++; | ||
| if (seconds >= 60) minutes++; | ||
| if (minutes >= 60) hours++; | ||
| if (hours >= 24) days++; | ||
| if (days === 31) months = 1; | ||
| if (days === 366) years = 1; | ||
| if (days > 31 && days < 366) { | ||
| var factor = Math.floor(days / 30); | ||
| var maxMonths = Math.ceil(days / 30); | ||
| months = days >= 30 * factor ? maxMonths : maxMonths - 1; | ||
| } | ||
| if (days > 365) { | ||
| var _factor = Math.floor(days / 365); | ||
| var _maxMonths = Math.ceil(days / 365); | ||
| years = days >= 365 * _factor ? _maxMonths : _maxMonths - 1; | ||
| } | ||
| if (years > 0) return humanise(TimeUnit.Year, tense, years); | ||
| if (months > 0) return humanise(TimeUnit.Month, tense, months); | ||
| if (days > 0) return humanise(TimeUnit.Day, tense, days); | ||
| if (hours > 0) return humanise(TimeUnit.Hour, tense, hours); | ||
| if (minutes > 0) return humanise(TimeUnit.Minute, tense, minutes); | ||
| if (seconds > 0) return humanise(TimeUnit.Second, tense, seconds); | ||
| return humanise(TimeUnit.Millisecond, tense, 0); | ||
| } | ||
| }]); | ||
| return DefaultDateTimeHumanizeStrategy; | ||
| }(); | ||
| exports["default"] = DefaultDateTimeHumanizeStrategy; |
| "use strict"; | ||
| var _chai = require("chai"); | ||
| var _DefaultDateTimeHumanizeStrategy = _interopRequireDefault(require("./DefaultDateTimeHumanizeStrategy")); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
| describe("DefaultDateTimeHumanizeStrategy", function () { | ||
| var _DefaultDateTimeHuman = new _DefaultDateTimeHumanizeStrategy["default"](), | ||
| humanize = _DefaultDateTimeHuman.humanize; | ||
| it("should return now for anything less than one second ago", function () { | ||
| (0, _chai.expect)(humanize(new Date(Date.now() - 500), new Date())).to.equal("now"); | ||
| }); | ||
| it("should return now for anything less than one second in the future", function () { | ||
| (0, _chai.expect)(humanize(new Date(Date.now() + 500), new Date())).to.equal("now"); | ||
| }); | ||
| it("should floor the input", function () { | ||
| (0, _chai.expect)(humanize(new Date(Date.now() + 999), new Date())).to.equal("now"); | ||
| }); | ||
| it("should handle one second", function () { | ||
| (0, _chai.expect)(humanize(new Date(Date.now() + 1000), new Date())).to.equal("in one second"); | ||
| }); | ||
| it("should handle 30 seconds", function () { | ||
| (0, _chai.expect)(humanize(new Date(Date.now() + 30000), new Date())).to.equal("in 30 seconds"); | ||
| }); | ||
| it("should handle minutes", function () { | ||
| (0, _chai.expect)(humanize(new Date(Date.now() + 60000), new Date())).to.equal("in one minute"); | ||
| }); | ||
| it("should handle hours", function () { | ||
| (0, _chai.expect)(humanize(new Date(Date.now() + 3600000), new Date())).to.equal("in one hour"); | ||
| }); | ||
| }); |
| export default interface IDateTimeHumanizeStrategy { | ||
| /** | ||
| * Calculates the distance of time in words between two provided dates | ||
| */ | ||
| humanize(input: Date, comparisonBase: Date): string; | ||
| } |
| "use strict"; |
| import IDateTimeHumanizeStrategy from "./IDateTimeHumanizeStrategy"; | ||
| export { default as IDateTimeHumanizeStrategy } from "./IDateTimeHumanizeStrategy"; | ||
| export default function humanize(time: Date, from: Date, strategy: IDateTimeHumanizeStrategy): string; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports["default"] = humanize; | ||
| Object.defineProperty(exports, "IDateTimeHumanizeStrategy", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _IDateTimeHumanizeStrategy["default"]; | ||
| } | ||
| }); | ||
| var _IDateTimeHumanizeStrategy = _interopRequireDefault(require("./IDateTimeHumanizeStrategy")); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
| function humanize(time) { | ||
| var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date(); | ||
| var strategy = arguments.length > 2 ? arguments[2] : undefined; | ||
| return strategy.humanize(time, from); | ||
| } |
| export { default as words } from "./words"; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| Object.defineProperty(exports, "words", { | ||
| enumerable: true, | ||
| get: function get() { | ||
| return _words["default"]; | ||
| } | ||
| }); | ||
| var _words = _interopRequireDefault(require("./words")); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } |
| export default function words(number: number, ordinal?: boolean): string; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports["default"] = words; | ||
| var ordinalExceptions = { | ||
| 1: "first", | ||
| 2: "second", | ||
| 3: "third", | ||
| 5: "fifth", | ||
| 8: "eighth", | ||
| 9: "ninth", | ||
| 12: "twelfth" | ||
| }; | ||
| var unitsMap = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]; | ||
| var tensMap = ["zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]; | ||
| function getUnitValue(number, ordinal) { | ||
| if (ordinal) { | ||
| if (number in ordinalExceptions) return ordinalExceptions[number]; | ||
| return unitsMap[number] + "th"; | ||
| } | ||
| return unitsMap[number]; | ||
| } | ||
| function removeOnePrefix(words) { | ||
| if (words.startsWith("one")) return words.substring(4); | ||
| return words; | ||
| } | ||
| function words(number) { | ||
| var ordinal = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
| number = Math.floor(number); | ||
| if (number === 0) return getUnitValue(0, ordinal); | ||
| if (number < 0) return "minus " + words(-number); | ||
| var parts = []; | ||
| if (number / 1e18 >= 1) { | ||
| parts.push(words(number / 1e18) + " quintillion"); | ||
| number %= 1e18; | ||
| } | ||
| if (number / 1e15 >= 1) { | ||
| parts.push(words(number / 1e15) + " quadrillion"); | ||
| number %= 1e15; | ||
| } | ||
| if (number / 1e12 >= 1) { | ||
| parts.push(words(number / 1e12) + " trillion"); | ||
| number %= 1e12; | ||
| } | ||
| if (number / 1e9 >= 1) { | ||
| parts.push(words(number / 1e9) + " billion"); | ||
| number %= 1e9; | ||
| } | ||
| if (number / 1e6 >= 1) { | ||
| parts.push(words(number / 1e6) + " million"); | ||
| number %= 1e6; | ||
| } | ||
| if (number / 1e3 >= 1) { | ||
| parts.push(words(number / 1e3) + " thousand"); | ||
| number %= 1e3; | ||
| } | ||
| if (number / 100 >= 1) { | ||
| parts.push(words(number / 100) + " hundred"); | ||
| number %= 100; | ||
| } | ||
| if (number > 0) { | ||
| if (parts.length !== 0) parts.push("and"); | ||
| if (number < 20) parts.push(getUnitValue(number, ordinal));else { | ||
| var lastPart = tensMap[Math.floor(number / 10)]; | ||
| if (number % 10 > 0) lastPart += "-" + getUnitValue(number % 10, ordinal);else if (ordinal) lastPart = lastPart.replace(/y$/, "") + "ieth"; | ||
| parts.push(lastPart); | ||
| } | ||
| } else if (ordinal) parts[parts.length - 1] += "th"; | ||
| var toWords = parts.join(" "); | ||
| if (ordinal) toWords = removeOnePrefix(toWords); | ||
| return toWords; | ||
| } |
| export {}; |
| "use strict"; | ||
| var _chai = require("chai"); | ||
| var _words = _interopRequireDefault(require("./words")); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
| describe("words", function () { | ||
| describe("to words", function () { | ||
| it("should handle a single digit", function () { | ||
| (0, _chai.expect)((0, _words["default"])(1)).to.equal("one"); | ||
| }); | ||
| it("should handle a multiple of ten", function () { | ||
| (0, _chai.expect)((0, _words["default"])(10)).to.equal("ten"); | ||
| }); | ||
| it("should handle a teen", function () { | ||
| (0, _chai.expect)((0, _words["default"])(11)).to.equal("eleven"); | ||
| }); | ||
| it("should handle another multiple of ten", function () { | ||
| (0, _chai.expect)((0, _words["default"])(20)).to.equal("twenty"); | ||
| }); | ||
| it("should handle a double digit number", function () { | ||
| (0, _chai.expect)((0, _words["default"])(22)).to.equal("twenty-two"); | ||
| }); | ||
| it("should handle a three digit number", function () { | ||
| (0, _chai.expect)((0, _words["default"])(122)).to.equal("one hundred and twenty-two"); | ||
| }); | ||
| it("should handle a multiple of 100", function () { | ||
| (0, _chai.expect)((0, _words["default"])(100)).to.equal("one hundred"); | ||
| }); | ||
| it("should handle a four digit number", function () { | ||
| (0, _chai.expect)((0, _words["default"])(3501)).to.equal("three thousand five hundred and one"); | ||
| }); | ||
| it("should handle a six digit number", function () { | ||
| (0, _chai.expect)((0, _words["default"])(500505)).to.equal("five hundred thousand five hundred and five"); | ||
| }); | ||
| }); | ||
| describe("ordinal", function () { | ||
| it("should handle zero", function () { | ||
| (0, _chai.expect)((0, _words["default"])(0, true)).to.equal("zeroth"); | ||
| }); | ||
| it("should handle 1st", function () { | ||
| (0, _chai.expect)((0, _words["default"])(1, true)).to.equal("first"); | ||
| }); | ||
| it("should handle 2nd", function () { | ||
| (0, _chai.expect)((0, _words["default"])(2, true)).to.equal("second"); | ||
| }); | ||
| it("should handle 3rd", function () { | ||
| (0, _chai.expect)((0, _words["default"])(3, true)).to.equal("third"); | ||
| }); | ||
| it("should handle th", function () { | ||
| (0, _chai.expect)((0, _words["default"])(4, true)).to.equal("fourth"); | ||
| }); | ||
| it("should handle teens", function () { | ||
| (0, _chai.expect)((0, _words["default"])(12, true)).to.equal("twelfth"); | ||
| }); | ||
| it("should handle tens", function () { | ||
| (0, _chai.expect)((0, _words["default"])(23, true)).to.equal("twenty-third"); | ||
| }); | ||
| it("should handle hundreds", function () { | ||
| (0, _chai.expect)((0, _words["default"])(121, true)).to.equal("hundred and twenty-first"); | ||
| }); | ||
| it("should handle thousands", function () { | ||
| (0, _chai.expect)((0, _words["default"])(1021, true)).to.equal("thousand and twenty-first"); | ||
| }); | ||
| }); | ||
| }); |
| export * from "./dist/number"; | ||
| export {}; |
| module.exports = require("./dist/number"); |
+2
-2
| { | ||
| "name": "@alduino/humanizer", | ||
| "version": "1.0.2", | ||
| "version": "1.1.0", | ||
| "description": "Javascript port of Humanizer.NET", | ||
@@ -28,5 +28,5 @@ "main": "dist/index.js", | ||
| "@babel/core": "^7.10.4", | ||
| "@babel/plugin-proposal-class-properties": "^7.10.4", | ||
| "@babel/preset-env": "^7.10.4", | ||
| "@babel/preset-typescript": "^7.10.4", | ||
| "@babel/plugin-proposal-class-properties": "^7.10.4", | ||
| "@types/chai": "^4.2.11", | ||
@@ -33,0 +33,0 @@ "@types/mocha": "^7.0.2", |
206666
6.47%70
34.62%1589
22.32%