Skip to content

Instantly share code, notes, and snippets.

View nixjs's full-sized avatar
:octocat:
Thanks for visiting

Nghi Nguyen nixjs

:octocat:
Thanks for visiting
View GitHub Profile
@nixjs
nixjs / enumify.ts
Created April 26, 2023 14:13
Convert array of strings into enum
type ValuesOfArray<T extends ReadonlyArray<any>> = T[number]
type ToObj<K extends string> = {
[P in K]: P
}
export const generate = <T extends readonly any[], K extends ValuesOfArray<T>>(
arr: T,
start?: number,
convention?: number
@nixjs
nixjs / generate.ts
Created November 4, 2022 13:10
Generate color by name
export function getColor(name: string): string {
const firstAlphabet = name.charAt(0).toLowerCase()
// get the ASCII code of the character
const asciiCode = firstAlphabet.charCodeAt(0)
// number that contains 3 times ASCII value of character -- unique for every alphabet
const colorNum = asciiCode.toString() + asciiCode.toString() + asciiCode.toString()
const num = Math.round(0xffffff * parseInt(colorNum))
const prefix = (num >> 16) & 255
// Here's a similar example which uses a class:
class AddNumbers {
private n: number;
constructor(start = 0) {
this.n = start;
}
export function isArray(val: any): boolean {
return Array.isArray(val)
}
export function isPlainObject(val: any): boolean {
if (toString.call(val) !== '[object Object]') {
return false
}
const prototype: Record<string, (...args: any[]) => any> = Object.getPrototypeOf(val)
@nixjs
nixjs / generateColor.js
Last active May 31, 2021 16:04
Generate color by name
import CryptoJS from 'crypto-js';
const toHue = value => {
let hash = 0;
if (value.length === 0) return hash;
for (let i = 0; i < value.length; i += 1) {
hash = value.charCodeAt(i) + ((hash << 5) - hash);
hash = hash & hash;
}
return hash % 360;

First Method

Deleting the .git folder may cause problems in our git repository. If we want to delete all of our commits history, but keep the code in its current state, try this:

# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH

# Add all the files:
git add -A