Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
typedconverter
Advanced tools
Convert object into classes match with TypeScript type annotation
import { convert } from "typedconverter";
const numb = await convert("12345", { type: Number }) //return number 12345
const numb = await convert("YES", { type: Boolean }) //return true
const numb = await convert("2019-2-2", { type: Date }) //return date 1/1/2019
Expected type can be specified in the configuration, than you can omit expected type on the second parameter of the convert
function. Useful when you want to covert several times without specifying expected type.
import createConverter from "typedconverter";
const convert = createConverter({type: Number})
const numb = await convert("12345")
const numb1 = await convert("-12345")
const numb2 = await convert("12345.123")
TypedConvert uses tinspector to get type metadata, so it aware about TypeScript type annotation.
import {convert} from "typedconverter";
import reflect from "tinspector"
@reflect.parameterProperties()
class AnimalClass {
constructor(
public id: number,
public name: string,
public deceased: boolean,
public birthday: Date
) { }
}
//return instance of AnimalClass with appropriate properties type
const data = await convert({
id: "200",
name: "Mimi",
deceased: "ON",
birthday: "2018-2-2" },
{ type: AnimalClass })
Convert into array by providing array of type in the expected type.
import {convert} from "typedconverter";
const numb = await convert(["1", "2", "-3"], { type: [Number] })
Nested child array need to be decorate for TypeScript added design data type
import {convert} from "typedconverter";
@reflect.parameterProperties()
class Tag {
constructor(
public name: string,
) { }
}
@reflect.parameterProperties()
class Animal {
constructor(
public name: string,
@reflect.array(Tag)
public tags: Tags
) { }
}
//tags is instance of Tag class
const numb = await convert({name: "Mimi", tags: [{name: "Susi"}, {name: "Lorem"}]}, { type: Animal })
Useful when converting data from url encoded, where single value could be a single array.
const b = await convert("1", { type: [Number], guessArrayElement: true }) //ok [1]
Visitors executed after conversion process traverse through properties / array element. Invocation can be multiple and run in sequence the last sequence will execute the converter. Visitors work like Plumier middleware
Signature of Visitor is like below:
type Visitor = (invocation: VisitorInvocation) => VisitorResult
Visitor is a function receive two parameters value
and invocation
.
invocation
next invocationExample:
import createConverter, { Result, VisitorInvocation } from "typedconverter"
const olderThanEightTeen = (i: VisitorInvocation) => {
if (i.type === Number && i.value < 18)
return Result.error(i.path, "Must be older than 18")
else
return i.proceed()
}
const convert = createConverter({ type: Number, visitors: [olderThanEightTeen] })
const result = convert("40") // { value: 40 }
const other = convert("12") // { issues: [{path: "", messages: ["Must be older than 18"]}] }
FAQs
Convert object into classes match with TypeScript type annotation
The npm package typedconverter receives a total of 1 weekly downloads. As such, typedconverter popularity was classified as not popular.
We found that typedconverter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.