@kodex-data/prototypes
Version:
A library of TypeScript prototypes that extend built-in types in JavaScript, providing additional functionality to make development easier and more efficient. This library includes prototypes for working with big numbers, Ethereum Virtual Machine (EVM) fu
65 lines (45 loc) • 3.83 kB
Markdown
# -data/cid-prototypes
`yarn add -data/cid-prototypes` or `npm install @kodex-data/cid-prototypes`
## Documentation for CID parsing and manipulation functions
This module exports several functions and interfaces related to parsing, formatting, and manipulating CID (Content ID) instances.
```typescript
import '@kodex-data/cid-prototypes'
import { CID } from 'multiformats'
const cids = 'bafybeih3qajng5kyjv4apiiughyjhflfc2kn747ggujq7d6ulkritfqx4e'.getCIDs()
const fromHEX = `f01701220fb8012d375584d7807a11431f09395651694dff3e635130f8fd45aa2899617e1`.parseCID()
const toV0 = CID.parse(cids.v0)
console.log(toV0.toV1().equals(fromHEX), cids.v0)
```
### Exported Constants
* `VERSION`: A constant string representing the version of this module.
### Exported Interfaces
* `CIDs`: An interface representing the different versions of a CID. It contains the following properties:
* `v0`: A string representing the version 0 CID.
* `v1`: A string representing the version 1 CID.
* `hex`: A string representing the hexadecimal version of a CID.
### Global Extensions to the String Prototype
This module extends the global `String` prototype with the following functions:
* `parseCID(version?: CIDVersions): CID`: A function that parses a string as a CID instance. If the `version` parameter is provided, it will parse the string as the specified CID version.
* `isCID(version?: CIDVersions): boolean`: A function that checks whether a string is a valid CID. If the `version` parameter is provided, it will check whether the string is a valid CID of the specified version.
* `getCIDVersion(): CIDVersions`: A function that returns the version of a CID as a string.
* `getCIDs(): CIDs`: A function that formats a CID string as all possible versions of the CID.
* `findCIDs(parse?: boolean): string[] | CID[]`: A function that finds all CIDs in a given string. If the `parse` parameter is `true`, it will return an array of CID instances. Otherwise, it will return an array of strings representing the found CIDs.
### Multiformats CID Extensions
This module extends the `CID` interface from the `multiformats` module with the following function:
* `getCIDs(): CIDs`: A function that formats a CID as all possible versions of the CID.
### Exported Functions
This module also exports several utility functions:
* `CID_V1`: A regular expression that matches version 1 CIDs.
* `CID_V0`: A regular expression that matches version 0 CIDs.
* `matcher(regex: RegExp, txt: string): string[]`: A function that returns an array of strings matching the given regular expression.
* `extractIpfsHashes(uri: string): string[]`: A function that extracts IPFS hashes from a URI string.
* `extractIpfsHash(uri: string): string`: A function that extracts the first IPFS hash from a URI string.
* `findCIDs(txt: string): string[]`: A function that finds all CIDs in a given string.
* `isHEX(input: string): boolean`: A function that checks whether a string is a valid hexadecimal string.
* `toHEX(input: string): string`: A function that converts a string to hexadecimal format.
* `fromHEX(input: string): string`: A function that converts a hexadecimal string to regular string format.
* `isV0(query: string): boolean`: A function that checks whether a string is a version 0 CID.
* `isV1(query: string): boolean`: A function that checks whether a string is a version 1 CID.
* `isHexCID(input: string): boolean`: A function that checks whether a string is a valid hexadecimal CID.
* `isCID(input: string, version?: CIDVersions): boolean`: A function that checks whether a string is a valid CID. If the `version` parameter is provided, it will check whether the string is a valid CID of the specified version.
* `getCIDVersion(input: string): CIDVersions`: A function that returns the version of a CID string as a string.