Skip to content

Commit

Permalink
feat: Add getColorByInternalName utility function (#163)
Browse files Browse the repository at this point in the history
feat: Add getColorByInternalName utility function
  • Loading branch information
engfragui authored Oct 13, 2022
1 parent e867903 commit 5ca839b
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@doist/todoist-api-typescript",
"version": "2.0.7",
"version": "2.1.0",
"description": "A typescript wrapper for the Todoist REST API.",
"author": "Doist developers",
"repository": "[email protected]:doist/todoist-api-typescript.git",
Expand Down
21 changes: 21 additions & 0 deletions src/types/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,29 @@ export const User = Record({
export type User = Static<typeof User>

export type Color = {
/**
* @deprecated No longer used
*/
id: number
/**
* The key of the color (i.e. 'berry_red')
*/
key: string
/**
* The display name of the color (i.e. 'Berry Red')
*/
displayName: string
/**
* @deprecated Use {@link Color.displayName} instead
*/
name: string
/**
* The hex value of the color (i.e. '#b8255f')
*/
hexValue: string
/**
* @deprecated Use {@link Color.hexValue} instead
*/
value: string
}

Expand Down
43 changes: 39 additions & 4 deletions src/utils/colors.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
import { berryRed, charcoal, taupe, getColorById } from './colors'
import {
berryRed,
taupe,
getColorById,
getColorByName,
getColorByKey,
defaultColor,
} from './colors'

describe('getColor', () => {
describe('getColorById', () => {
const colorTheories = [
[0, charcoal], // out of range, defaulted
[0, defaultColor], // out of range, defaulted
[30, berryRed],
[49, taupe],
[999, charcoal], // out of range, defaulted
[999, defaultColor], // out of range, defaulted
] as const

test.each(colorTheories)('id %p returns color %p', (id, expected) => {
const color = getColorById(id)
expect(color).toEqual(expected)
})
})

describe('getColorByName', () => {
const colorTheories = [
['Berry Red', berryRed],
['Taupe', taupe],
['berry_red', defaultColor], // does not exist, defaulted
['Some non existing color', defaultColor], // does not exist, defaulted
] as const

test.each(colorTheories)('name %p returns color %p', (name, expected) => {
const color = getColorByName(name)
expect(color).toEqual(expected)
})
})

describe('getColorByKey', () => {
const colorTheories = [
['berry_red', berryRed],
['taupe', taupe],
['Berry Red', defaultColor], // does not exist, defaulted
['Some non existing color', defaultColor], // does not exist, defaulted
] as const

test.each(colorTheories)('key %p returns color %p', (key, expected) => {
const color = getColorByKey(key)
expect(color).toEqual(expected)
})
})
199 changes: 176 additions & 23 deletions src/utils/colors.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,165 @@
import { Color } from '../types'

export const berryRed: Color = { name: 'Berry Red', id: 30, value: '#b8255f' }
export const red: Color = { name: 'Red', id: 31, value: '#db4035' }
export const orange: Color = { name: 'Orange', id: 32, value: '#ff9933' }
export const yellow: Color = { name: 'Yellow', id: 33, value: '#fad000' }
export const oliveGreen: Color = { name: 'Olive Green', id: 34, value: '#afb83b' }
export const limeGreen: Color = { name: 'Lime Green', id: 35, value: '#7ecc49' }
export const green: Color = { name: 'Green', id: 36, value: '#299438' }
export const mintGreen: Color = { name: 'Mint Green', id: 37, value: '#6accbc' }
export const turquoise: Color = { name: 'Turquoise', id: 38, value: '#158fad' }
export const skyBlue: Color = { name: 'Sky Blue', id: 39, value: '#14aaf5' }
export const lightBlue: Color = { name: 'Light Blue', id: 40, value: '#96c3eb' }
export const blue: Color = { name: 'Blue', id: 41, value: '#4073ff' }
export const grape: Color = { name: 'Grape', id: 42, value: '#884dff' }
export const violet: Color = { name: 'Violet', id: 43, value: '#af38eb' }
export const lavender: Color = { name: 'Lavender', id: 44, value: '#eb96eb' }
export const magenta: Color = { name: 'Magenta', id: 45, value: '#e05194' }
export const salmon: Color = { name: 'Salmon', id: 46, value: '#ff8d85' }
export const charcoal: Color = { name: 'Charcoal', id: 47, value: '#808080' }
export const gray: Color = { name: 'Gray', id: 48, value: '#b8b8b8' }
export const taupe: Color = { name: 'Taupe', id: 49, value: '#ccac93' }
export const berryRed: Color = {
id: 30,
key: 'berry_red',
displayName: 'Berry Red',
name: 'Berry Red',
hexValue: '#b8255f',
value: '#b8255f',
} as const
export const red: Color = {
id: 31,
key: 'red',
displayName: 'Red',
name: 'Red',
hexValue: '#db4035',
value: '#db4035',
} as const
export const orange: Color = {
id: 32,
key: 'orange',
displayName: 'Orange',
name: 'Orange',
hexValue: '#ff9933',
value: '#ff9933',
} as const
export const yellow: Color = {
id: 33,
key: 'yellow',
displayName: 'Yellow',
name: 'Yellow',
hexValue: '#fad000',
value: '#fad000',
} as const
export const oliveGreen: Color = {
id: 34,
key: 'olive_green',
displayName: 'Olive Green',
name: 'Olive Green',
hexValue: '#afb83b',
value: '#afb83b',
} as const
export const limeGreen: Color = {
id: 35,
key: 'lime_green',
displayName: 'Lime Green',
name: 'Lime Green',
hexValue: '#7ecc49',
value: '#7ecc49',
} as const
export const green: Color = {
id: 36,
key: 'green',
displayName: 'Green',
name: 'Green',
hexValue: '#299438',
value: '#299438',
} as const
export const mintGreen: Color = {
id: 37,
key: 'mint_green',
displayName: 'Mint Green',
name: 'Mint Green',
hexValue: '#6accbc',
value: '#6accbc',
} as const
export const turquoise: Color = {
id: 38,
key: 'turquoise',
displayName: 'Turquoise',
name: 'Turquoise',
hexValue: '#158fad',
value: '#158fad',
} as const
export const skyBlue: Color = {
id: 39,
key: 'sky_blue',
displayName: 'Sky Blue',
name: 'Sky Blue',
hexValue: '#14aaf5',
value: '#14aaf5',
} as const
export const lightBlue: Color = {
id: 40,
key: 'light_blue',
displayName: 'Light Blue',
name: 'Light Blue',
hexValue: '#96c3eb',
value: '#96c3eb',
} as const
export const blue: Color = {
id: 41,
key: 'blue',
displayName: 'Blue',
name: 'Blue',
hexValue: '#4073ff',
value: '#4073ff',
} as const
export const grape: Color = {
id: 42,
key: 'grape',
displayName: 'Grape',
name: 'Grape',
hexValue: '#884dff',
value: '#884dff',
} as const
export const violet: Color = {
id: 43,
key: 'violet',
displayName: 'Violet',
name: 'Violet',
hexValue: '#af38eb',
value: '#af38eb',
} as const
export const lavender: Color = {
id: 44,
key: 'lavender',
displayName: 'Lavender',
name: 'Lavender',
hexValue: '#eb96eb',
value: '#eb96eb',
} as const
export const magenta: Color = {
id: 45,
key: 'magenta',
displayName: 'Magenta',
name: 'Magenta',
hexValue: '#e05194',
value: '#e05194',
} as const
export const salmon: Color = {
id: 46,
key: 'salmon',
displayName: 'Salmon',
name: 'Salmon',
hexValue: '#ff8d85',
value: '#ff8d85',
} as const
export const charcoal: Color = {
id: 47,
key: 'charcoal',
displayName: 'Charcoal',
name: 'Charcoal',
hexValue: '#808080',
value: '#808080',
} as const
export const gray: Color = {
id: 48,
key: 'gray',
displayName: 'Gray',
name: 'Gray',
hexValue: '#b8b8b8',
value: '#b8b8b8',
} as const
export const taupe: Color = {
id: 49,
key: 'taupe',
displayName: 'Taupe',
name: 'Taupe',
hexValue: '#ccac93',
value: '#ccac93',
} as const

export const colors = [
berryRed,
Expand All @@ -42,14 +182,27 @@ export const colors = [
charcoal,
gray,
taupe,
]
] as const

export const defaultColor: Color = charcoal

/**
* @deprecated Use {@link getColorByKey} instead
*/
export function getColorById(colorId: number): Color {
const color = colors.find((color) => color.id === colorId)
return color ?? charcoal
return color ?? defaultColor
}

/**
* @deprecated Use {@link getColorByKey} instead
*/
export function getColorByName(colorName: string): Color {
const color = colors.find((color) => color.name === colorName)
return color ?? charcoal
return color ?? defaultColor
}

export function getColorByKey(colorKey: string): Color {
const color = colors.find((color) => color.key === colorKey)
return color ?? defaultColor
}

0 comments on commit 5ca839b

Please sign in to comment.