GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - productive-codebases/toolbox: Some useful functions for productive codebases.

This repository was archived by the owner on Sep 28, 2026. It is now read-only.
BranchesTags
Open more actions menu

Latest commit

 

History

81 Commits
81 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
.husky
.husky
 
 
src
src
 
 
.eslintrc.json
.eslintrc.json
 
 
.gitignore
.gitignore
 
 
.npmignore
.npmignore
 
 
.prettierrc
.prettierrc
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
jest.config.js
jest.config.js
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
tsconfig.json
tsconfig.json
 
 
tsconfig.node.json
tsconfig.node.json
 
 
vite.config.ts
vite.config.ts
 
 

Repository files navigation

Toolbox

Some useful functions and types.

Motivation

Set of functions and types used across the different projects of Productive Codebases.

Prerequisites

Typescript is not mandatory but highly recommended.

Installation

npm install @productive-codebases/toolbox

Exposed tooling

Conditions

assertUnreachableCase

Ensures that all cases of a switch are implemented by returning a never type in the default case.

Usage:

switch (unionOfValues) {
  case 'value1': {
    // ...
  }

  case 'value2': {
    // ...
  }

  default: {
    assertUnreachableCase(unionOfValues)
  }
}

Entities

createEntity and createEntities

Instanciate entities from a litteral objects.

Usage:

const user = createEntity(EntityUser, {
  id: '1',
  username: 'John'
})

const users = createEntities(EntityUser, [
  {
    id: '1',
    username: 'John'
  },
  {
    id: '2',
    username: 'T1000'
  }
])

StrictObject

Typed listing of keys, values and entries from an object.

Usage:

const user = {
  id: '1',
  username: 'John'
}

StrictObject.keys(user)     // ['id', 'username']
StrictObject.values(user)   // ['1', 'John']
StrictObject.entries(user)  // [['id', '1'], ['username', 'John']]

Objects

deepMerge

Deeply merges multiple objects.

Usage:

const mergedObjects = deepMerge([object1, object2], optionalOptions)

Arrays

ensureArray

Ensures that a value is an array.

Usage:

ensureArray(x).map(...)

Sets

ensureSet

Ensures that a value is a set.

Usage:

ensureSet(x).forEach(...)

Maps

addSetValueToMap, removeSetValueToMap

Add or remove a Set value to a map.

Usage:

const map = new Map<string, Set<number>>()

addSetValueToMap(map, 'scores', 42)
addSetValueToMap(map, 'scores', 92)

const numbers = map.get('scores')

numbers.size // 2

indexEntitiesToMap, appendEntitiesToMap

Index / Append entities (objects) to a map.

Usage:

const users = [
  {
    id: 1,
    name: 'Bob'
  },
  {
    id: 2,
    name: 'Alice'
  },
  {
    id: 3,
    name: 'John'
  }
]

const usersMap = indexEntitiesToMap(users, 'id')
const bobUser = usersMap.get(1)
const users = [
  {
    id: 1,
    name: 'Bob',
    lastName: 'Foo'
  },
  {
    id: 2,
    name: 'Alice',
    lastName: 'Foo'
  },
  {
    id: 3,
    name: 'John',
    lastName: 'Bar'
  }
]

const usersMap = appendEntitiesToMap(users, 'lastName')
const usersHavingFoo = usersMap.get('Foo')

usersHavingFoo.size // 2

Assertions

isDefined

Returns a type predicate to remove undefined or nullable values of an array.

Usage:

if (isDefined(x)) {
  //...
}
const values = [null, undefined, '', 'value', 0].filter(isDefined)

values.length // 3

isNotFalsy, isNotFalsy

Returns a type predicate to filter falsy values of an array.

Usage:

if (isNotFalsy(x)) {
  //...
}
const values = [null, undefined, '', 'value', 0].filter(filterFalsies)

values.length // 1

isTruthy

Return true if the value is truthy.

Usage:

if (isTruthy(x)) {
  //...
}

Tooling

setupLogger

Return a composite function allowing to log message from a defined logger mapping.

const loggerMapping = {
  server: {
    express: 'express',
    middleware: 'middleware'
  },
  client: {
    react: 'react',
    store: 'store'
  }
}

const { newLogger } = setupLogger(loggerMapping)
const logger = newLogger('server')('middleware')

logger('info')('Send request')
logger('error')('Error 500')

You can enable / disable logger messages by adding a debug property in local storage. Example:

// Will show only log messages for the server middleware:
localStorage.set('debug', 'server:middleware:*')

MetaData

Metadata-like data storage.

Usage:

interface IMetaData {
  data: string
}

const metadata = new MetaData<IMetaData>()
metadata.set({ data: 'foo' })
const value = metadata.get('data')

Types

Maybe

Define a value that can be null.

Usage:

type Value = Maybe<string>

MaybeUndef

Define a value that can be undefined.

Usage:

type Value = MaybeUndef<string>

Perhaps

Define a value that can be null or undefined.

Usage:

type Value = Perhaps<string>

PropertiesNullable

Define an object with all properties as nullable values.

Usage:

interface IUser = {
  id: number
  name: string
}

const user: PropertiesNullable<IUser> = {
  id: null,
  name: null
}

PropertiesNonNullable

Define an object with all properties as non-nullable values.

Usage:

interface INullableUser = {
  id: Maybe<number>
  name: Maybe<string>
}

const user: PropertiesNonNullable<INullableUser> = {
  id: 1,
  name: null  // Error
}

PartialDeep

Partial applyed deeply.

Usage:

interface IUser = {
  id: number
  name: string
  address: {
    city: string
    country: string
  }
}

const user: PartialDeep<IUser> = {
  id: 1,
  address: {
    country: 'France'
  }
}

About

Some useful functions for productive codebases.

Resources

Readme
MIT license
Activity
Custom properties

Stars

5 stars

Watchers

1 watching

Forks

3 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.