How to use the hashids/cjs function in hashids

To help you get started, we’ve selected a few hashids examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github staart / api / src / helpers / utils.ts View on Github external
import dns from "dns";
import Joi from "@hapi/joi";
import { getOrganizationIdFromUsername } from "../crud/organization";
import { Request, Response } from "express";
import slugify from "slugify";
import cryptoRandomString from "crypto-random-string";
import { Tokens } from "../interfaces/enum";
import { ApiKeyResponse } from "./jwt";
import { isMatch } from "matcher";
import Hashids from "hashids/cjs";
import { getUserIdFromUsername } from "../crud/user";
import { HASH_IDS, HASH_ID_PREFIX } from "../config";
import systemInfo from "systeminformation";
import pkg from "../../package.json";

const hashIds = new Hashids(
  HASH_IDS,
  10,
  "abcdefghijklmnopqrstuvwxyz1234567890"
);

/**
 * Capitalize each first letter in a string
 */
export const capitalizeEachFirstLetter = (string: string) =>
  (string = string
    .toLowerCase()
    .split(" ")
    .map(s => s.charAt(0).toUpperCase() + s.toLowerCase().substring(1))
    .join(" "));

/**
github staart / api / src / helpers / utils.ts View on Github external
import anonymize from "ip-anonymize";
import { User } from "../interfaces/tables/user";
import dns from "dns";
import Joi from "@hapi/joi";
import { getOrganizationIdFromUsername } from "../crud/organization";
import { Request, Response } from "express";
import slugify from "slugify";
import cryptoRandomString from "crypto-random-string";
import { Tokens } from "../interfaces/enum";
import { ApiKeyResponse } from "./jwt";
import { isMatch } from "matcher";
import Hashids from "hashids/cjs";
import { getUserIdFromUsername } from "../crud/user";
import { HASH_IDS, HASH_ID_PREFIX } from "../config";

const hashIds = new Hashids(
  HASH_IDS,
  10,
  "abcdefghijklmnopqrstuvwxyz1234567890"
);

/**
 * Capitalize each first letter in a string
 */
export const capitalizeEachFirstLetter = (string: string) =>
  (string = string
    .toLowerCase()
    .split(" ")
    .map(s => s.charAt(0).toUpperCase() + s.toLowerCase().substring(1))
    .join(" "));

/**
github opencollective / opencollective-api / server / graphql / v2 / identifiers.js View on Github external
const getInstance = type => {
  let instance = instances[type];
  if (!instance) {
    instance = instances[type] = new Hashids(salt + type, 32, alphabet);
  }
  return instance;
};
github birkir / prime / packages / prime-core / src / modules / internal / resolvers / AccessTokenResolver.ts View on Github external
public async createAccessToken(
    @Arg('input') input: AccessTokenInput,
    @Ctx() context: Context
  ): Promise {
    const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_.';
    const salt = String(process.env.HASHID_SALT || 'keyboard dart cat');
    const hash = crypto.createHmac('sha512', salt);
    const hashid = new Hashids(salt, 108, alphabet);
    hash.update(`${Math.floor(1000 * Math.random())}|${Date.now()}`);
    const accessToken = this.accessTokenRepository.create(input);
    const tokenSeed = hash.digest('hex').match(/.{1,8}/g) || [];
    const token = hashid.encode(tokenSeed.map(num => parseInt(num, 16)));
    if (!tokenSeed.length || token === '') {
      throw new Error('Failed to generate access token');
    }
    accessToken.token = token;
    accessToken.userId = context.user.id;
    await this.accessTokenRepository.save(accessToken);
    return accessToken;
  }
github birkir / prime / packages / prime-core / src / utils / getUniqueHashId.ts View on Github external
import Hashids from 'hashids/cjs';
import { Repository } from 'typeorm';

const hashid = new Hashids(process.env.HASHID_SALT || 'SaltingTheHash', 10);

export const getUniqueHashId = async (repository: Repository, fieldName: string = 'id') => {
  const id = hashid.encode(Date.now());
  const count = await repository.count({
    where: { [fieldName]: id },
  });
  return count === 0 ? id : getUniqueHashId(repository, fieldName);
};

hashids

Generate YouTube-like ids from numbers. Use Hashids when you do not want to expose your database ids to the user.

MIT
Latest version published 2 years ago

Package Health Score

62 / 100
Full package analysis

Popular hashids functions