Added types for identicon.js (#34286)

* added identicon.js types

* instead of exporting the PNGlib and Svg class they are now declared

* instead of classes interfaces are now used for PNGlib and Svg
This commit is contained in:
Dominik H
2019-03-29 17:28:47 -07:00
committed by Ron Buckton
parent dae28f3fcf
commit ec6e825ec9
4 changed files with 169 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
import Identicon, { Svg } from "identicon.js";
// create an identicon with only a hash and a size
new Identicon("d3b07384d113edec49eaa6238ad5ff00", 420).toString();
// create an identicon with a hash and other options and get the svg
const svg = new Identicon("d3b07384d113edec49eaa6238ad5ff00", {
background: [255, 255, 255, 255],
foreground: [0, 0, 0, 0],
margin: 0.05,
size: 64,
format: "svg",
}).image() as Svg;
svg.getDump();
// or get the base64 encoded svg
svg.getBase64();
+129
View File
@@ -0,0 +1,129 @@
// Type definitions for identicon.js 2.3
// Project: https://github.com/stewartlord/identicon.js
// Definitions by: D0miH <https://github.com/D0miH>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export type Color = [number, number, number, number];
export interface IdenticonOptions {
background?: Color;
foreground?: Color;
margin?: number;
size?: number;
format?: "svg" | "png";
}
export interface PNGlib {
width: number;
height: number;
depth: number;
/**
* Returns the index of a given pixel in the image data array.
* @param x The given x coordinate of the pixel.
* @param y The given y coordinate of the pixel.
*/
index(x: number, y: number): number;
/**
* Returns the image as a base64 encoded string.
*/
getBase64(): string;
/**
* Returns the png as a string.
*/
getDump(): string;
}
export interface Svg {
size: number;
foreground: Color;
background: Color;
rectangles: [
{
x: number;
y: number;
width: number;
height: number;
color: Color;
}
];
/**
* Returns a string with the structure 'rgb(r, g, b, a)'.
* @param red
* @param green
* @param blue
* @param alpha
*/
color(red: number, green: number, blue: number, alpha: number): string;
/**
* Returns the Svg as string.
*/
getDump(): string;
/**
* Returns the Svg as a base64 encoded string.
*/
getBase64(): string;
}
export default class Identicon {
hash: string;
foreground: Color;
background: Color;
size: number;
format: "svg" | "png";
margin: number;
constructor(hash: string, size?: number);
constructor(hash: string, options: IdenticonOptions);
/**
* Returns a new blank image as Svg or PNGlib.
*/
image(): Svg | PNGlib;
/**
* Returns a new image as Svg or PNGlib with the identicon applied.
*/
render(): Svg | PNGlib;
/**
* Places a rectangle at the given position with the given width, height and color in the image.
* @param x The x coordinate.
* @param y The y coordinate
* @param w The width.
* @param h The height.
* @param color The color.
* @param image The image.
*/
rectangle(
x: number,
y: number,
w: number,
h: number,
color: Color,
image: Svg | PNGlib
): void;
/**
* Converts from hsl to rgb.
* @param h hue
* @param s saturation
* @param b brightness
*/
hsl2rgb(h: number, s: number, b: number): [number, number, number];
/**
* Returns the image data as a string.
*/
toString(): string;
/**
* Returns true if the identicon is a Svg.
*/
isSvg(): boolean;
}
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"identicon.js-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }