{"id":16666700,"url":"https://github.com/nerdo/js-utils","last_synced_at":"2025-12-28T08:34:43.027Z","repository":{"id":40694704,"uuid":"233879602","full_name":"nerdo/js-utils","owner":"nerdo","description":"nerdo's JavaScript utilities","archived":false,"fork":false,"pushed_at":"2023-10-18T10:11:06.000Z","size":1413,"stargazers_count":0,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-19T16:36:00.997Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nerdo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-14T16:01:35.000Z","updated_at":"2022-02-16T05:39:21.000Z","dependencies_parsed_at":"2025-01-04T20:17:03.030Z","dependency_job_id":null,"html_url":"https://github.com/nerdo/js-utils","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nerdo%2Fjs-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nerdo%2Fjs-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nerdo%2Fjs-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nerdo%2Fjs-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nerdo","download_url":"https://codeload.github.com/nerdo/js-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243310605,"owners_count":20270820,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T11:11:50.245Z","updated_at":"2025-12-28T08:34:43.001Z","avatar_url":"https://github.com/nerdo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @nerdo/utils\n\u003e nerdo's JavaScript utilities.\n\nInstall it:\n\n```bash\nnpm install @nerdo/utils --save\n```\n### isDateObject(object)\n`import { isDateObject } from '@nerdo/utils'`\n\u003e Tests whether an object is a Date object.\n\n* `object` _{object}_: the object in question.\n\n`isDateObject(...) returns true if the object is a Date object, false otherwise.`\n\n### map(object, mapper)\n`import { map } from '@nerdo/utils'`\n\u003e A recursive mapping function for objects.\n\n* `object` _{object}_: the object to map.\n* `mapper` _{function}_: a function that handles the mapping.\n\n`map(...)` returns the object\n\n`map` traverses key-value pairs in an object recursively, calling the mapper on it and replacing each value with what the mapper returns.\n\n`mapper` has the signature `mapper(value, {object, path})`.\n\n* `value` _{mixed}_: the current value being mapped.\n* `object` _{object}_: the root object the mapping is being performed on.\n* `path`: _{array}_: the list of keys identifying to the current value being mapped.\n\nHere is simple example, filtering out all values that are numeric from the object:\n\n```js\nconst obj = {\n    name: 'John Doe',\n    age: 42,\n    dateOfBirth: new Date(1973, 1, 7),\n    misc: {\n        height: 136,\n        weight: 159\n    }\n}\nconst mapper = v =\u003e typeof v === 'number' ? undefined : v\nmap(obj, mapper)\nconsole.log(obj) // {name: 'John Doe', dateOfBirth: 'Wed Feb 07 1973...', misc: {}}\n```\n\nNote, that the mapper will _not_ traverse a value that is an object that has been removed by the mapper. Here is an example:\n\n```js\nconst obj = {\n    foo: {\n        removeMe: false,\n        bar: {\n            removeMe: true,\n            ignored: {\n                removeMe: true\n            }\n        }\n    }\n}\nconst mapper = function (v, {path}) {\n    console.log(path.join('.'))\n    return v.removeMe ? undefined : v\n}\nmapper(obj, mapper) // foo, foo.bar\nconsole.log(obj) // {foo: {removeMe: false }}\n```\n\nAlthough both `foo.bar` and `foo.bar.ignored` have been removed by the mapper, the console logs confirm that the mapper never actually iterated on `foo.bar.ignored`. It was removed because it was a property of `foo.bar`, which was removed.\n\n### clone(object)\n`import { clone } from '@nerdo/utils'`\n\u003e Returns a deep clone of the specified object\n\n* `object` _{object}_: the object to clone.\n\n`clone(...)` returns the deep cloned object\n\n```js\nconst obj = {\n  a: [\n    9,\n    {\n      foo: 'bar'\n    },\n    7\n  ]\n}\nconst cloned = clone(obj)\nconsole.log(cloned) // a deep clone of obj\n```\n\n### get(object, path, [defaultValue = _undefined_])\n`import { get } from '@nerdo/utils'`\n\u003e Returns the value in an object at the location specified by path.\n\n* `object` _{object}_: the object to query.\n* `path` _{string|array}_: the dotted-key string path or array path of the property to get.\n* `defaultValue` _{mixed}_ **[undefined]**: the default value to return if the property path cannot be fully resolved.\n\n`get(...)` returns the value, or `defaultValue` if the property path could not be fully resolved.\n\nThe path may be specified using either dotted-key or array path syntax. If the path cannot be fully resolved, the `defaultValue` is returned. For example:\n\n```js\nconst object = {a: [{b: {c: 3}}]}\nget(object, 'a[0].b.c') // 3\nget(object, ['a', 0, 'b', 'c']) // 3\nget(object, 'a.b.c', 555) // 5\n```\n\n### representMonth({ date, startingDayOfWeek })\n`import { representMonth } from '@nerdo/utils'`\n\u003e Generates an object representation of a calendar date.\n\n* `date` _{representMonth.DateAdapter}_ **[new representMonth.DateAdapter()]**: the date to be represented.\n* `startingDayOfWeek` _{representMonth.DayOfWeek}_ **[representMonth.DayOfWeek.Sunday]**: the starting day of the week.\n\nProperties:\n\n* `DateAdapter` _{Date}_: a constructor that is compatible with the JavaScript `Date` object.\n\n`representMonth(...)` returns an object with the following structure:\n\n* `date` _{representMonth.DateAdapter}_: the `representMonth.DateAdapter` instance of the date being represented.\n* `numberOfDays` _{number}_: the number of days in the month\n* `weeks` _{(number | null)[][]}_: the list of days in each week (i.e. 1 through 31 depending on the month). Each week array will always represent each of the 7 days and the first index of the week corresponds to the `startingDayOfWeek` argument. If a value is `null`, that particular day does not exist in the month. This is visually equivalent to when there are blank spaces in a calendar in the first and last weeks of a month that overlap with the previous month.\n* `prevMonthNumberOfDays` _{number}_: the number of days in the previous month\n* `prevMonthLastWeek` _{(number | null)[]}_: the last week of the previous month that overlaps with the first week of the current month.\n* `nextMonthNumberOfDays` _{number}_: the number of days in the next month\n* `nextMonthFirstWeek` _{(number | null)[]}_: the first week of the next month that overlaps with the last week of the current month.\n\nA typical usage of this is to render calendars. For example:\n\n```ts\nconst monthNames = [\n  'January',\n  'February',\n  'March',\n  'April',\n  'May',\n  'June',\n  'July',\n  'August',\n  'September',\n  'October',\n  'November',\n  'December'\n]\nconst year = 2020\nconst cellSize = 4\nconst calWidth = 7 * cellSize + 8\nmonthNames.forEach((monthName, index) =\u003e {\n  const label = `${monthName} ${year}`\n  console.log(`${' '.repeat((calWidth - label.length) / 2)}${label}`) // centers the label over the calendar\n  const representation = representMonth({date: new Date(year, index)}) //?\n  const renderData: (string | number | null)[][] = [['S', 'M', 'T', 'W', 'T', 'F', 'S']]\n  renderData\n    .concat(representation.weeks)\n    .map((week) =\u003e week\n      .map((day) =\u003e `${day || ''}`) // coerce each day to a string\n      .map((stringDay) =\u003e stringDay.padStart(cellSize, ' '))\n      .join(' ')\n    )\n    .forEach((weekString) =\u003e console.log(weekString))\n})\n```\n\nThe preceeding code results in console logging of each month of the 2020 calendar year.\n\nHere's a slightly more advanced example which uses the data to console log calendar months with previous and next months days showing up in square brackets (e.g. [30])\n\n```ts\nconst monthNames = [\n  'January',\n  'February',\n  'March',\n  'April',\n  'May',\n  'June',\n  'July',\n  'August',\n  'September',\n  'October',\n  'November',\n  'December'\n]\nconst year = 2020\nconst cellSize = 6\nconst calWidth = 7 * cellSize + 8\nmonthNames.forEach((monthName, index) =\u003e {\n  const label = `${monthName} ${year}`\n  console.log(`${' '.repeat((calWidth - label.length) / 2)}${label}`) // centers the label over the calendar\n  const representation = representMonth({date: new Date(year, index)}) //?\n  const renderData: (string | number | null)[][] = [['S', 'M', 'T', 'W', 'T', 'F', 'S']]\n  renderData\n    .concat(representation.weeks)\n    .map((week, weekIndex) =\u003e {\n      // checking weekIndex as if it were 1-based because we added the header for the week day labels\n      const maybeFirstWeek = weekIndex === 1 ? representation.prevMonthLastWeek : void 0\n      const maybeLastWeek = weekIndex === representation.weeks.length ? representation.nextMonthFirstWeek : void 0\n      const edgeWeek = maybeFirstWeek || maybeLastWeek\n      return week\n        .map((day, index) =\u003e { // coerce each day to a string\n          if (edgeWeek \u0026\u0026 edgeWeek[index]) return `[${edgeWeek[index] || ''}]`\n          return `${day || ''}`\n        }) \n        .map((stringDay) =\u003e stringDay.padStart(cellSize, ' '))\n        .join(' ')\n    })\n    .forEach((weekString) =\u003e console.log(weekString))\n})\n```\n\n### isLeapYear(year: number): boolean\n`import { isLeapYear } from '@nerdo/utils'`\n\u003e Whether or not the year is a leap year.\n\n* `year` _{number}_ the year\n\n### numDaysInMonth(month: number, year: number): number\n`import { numDaysInMonth } from '@nerdo/utils'`\n\u003e Gets the number of days in a calendar month.\n\n* `month` _{number}_ the month; 0 = January, 11 = December\n* `year` _{number}_ the year\n\n### DayOfWeek\n`import { DayOfWeek } from '@nerdo/utils'`\n\u003e An object representing the days of the week for use in representMonth().\n\nProperties:\n* `Sunday`\n* `Monday`\n* `Tuesday`\n* `Wednesday`\n* `Thursday`\n* `Saturday`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnerdo%2Fjs-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnerdo%2Fjs-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnerdo%2Fjs-utils/lists"}