Skip to content

Commit

Permalink
Update examples and calculateRarity implementation (#61)
Browse files Browse the repository at this point in the history
Signed-off-by: Michiel Mulders <[email protected]>
  • Loading branch information
michielmulders authored Apr 26, 2024
1 parent 75c4ff7 commit 4d3d11e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ const { localValidation } = require('@hashgraph/hedera-nft-sdk');
The `localValidation` expects a relative path to your metadata files to verify them. The function prints the warnings and errors for all JSON files it finds in the provided folder path. It also returns the validation results as an object in case you want to use the results in your code.

```js
const issues = localValidation('/Users/projects/nft/files');
const relativePathToFiles = './examples/local-metadata-validator/files';
const issues = localValidation(relativePathToFiles);
```

This package uses the `Validator` class explained in the [previous section](#token-metadata-validator).
Expand Down Expand Up @@ -443,13 +444,13 @@ Install the package:
npm i -s @hashgraph/hedera-nft-sdk
```

Import the package into your project and get `calculateRarity` function. Next, you need to pass an absolute path to a folder containing metadata JSON files.
Import the package into your project and get `calculateRarity` function. Next, you need to pass a relative path to a folder containing metadata JSON files.

```js
const { calculateRarity } = require('@hashgraph/hedera-nft-sdk');

const absolutePathToFiles = '/Users/myUser/hedera-nft-sdk/examples/rarity-score-calculation/files';
const results = calculateRarity(absolutePathToFiles);
const relativePathToFiles = './examples/local-metadata-validator/files';
const results = calculateRarity(relativePathToFiles);
console.log(results);
```

Expand Down
4 changes: 2 additions & 2 deletions examples/rarity-score-calculation/rarity-from-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const fs = require('fs');

function main() {
// Replace with absolute path to files folder
const absolutePathToFiles = '/Users/myUser/hedera-nft-sdk/examples/rarity-score-calculation/files';
const results = calculateRarity(absolutePathToFiles);
const relativePathToFiles = './examples/local-metadata-validator/files';
const results = calculateRarity(relativePathToFiles);
console.log(JSON.stringify(results, null, 4));

let flatData = results.reduce((arr, row) => {
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ export const readFiles = (dir: string, filenames: string[]): { filename: string;
* @param dir Absolute path to folder you want to validate
* @returns An array of filenames with extension
*/
export const getJSONFilesForDir = (dir: string): string[] => {
const directoryPath = dir;

const files = fs.readdirSync(directoryPath, { withFileTypes: true });
export const getJSONFilesForPath = (dir: string): string[] => {
const files = fs.readdirSync(dir, { withFileTypes: true });

const JSONFiles: string[] = [];
files.forEach((file) => {
Expand Down
6 changes: 3 additions & 3 deletions src/local-validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import path from 'path';
import { Validator } from '../validator/index';
import { readFiles, getJSONFilesForDir } from '../helpers/files';
import { readFiles, getJSONFilesForPath } from '../helpers/files';

import { ValidationResult, Instance } from '../types/validator';

Expand Down Expand Up @@ -47,12 +47,12 @@ const validateFiles = (files: File[]): ValidationResults => {
/**
* Validate files locally
*
* @param {string} relative Relative path to folder containing files
* @param {string} relativePath Relative path to folder containing files
* @returns {Object<filename<string>, validationResults<Object>>}
*/
const localValidation = (relativePath: string): ValidationResults => {
const absolutePath = path.resolve(relativePath); // convert relative path to absolute path
const filenames = getJSONFilesForDir(absolutePath);
const filenames = getJSONFilesForPath(absolutePath);
const filedata = readFiles(absolutePath, filenames);
const validationResults = validateFiles(filedata);

Expand Down
13 changes: 7 additions & 6 deletions src/rarity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* limitations under the License.
*
*/

import { readFiles, getJSONFilesForDir } from '../helpers/files';
import path from 'path';
import { readFiles, getJSONFilesForPath } from '../helpers/files';
import { RarityResult, AttributeConfig, ValueObject, NFTFile, TraitOccurrence } from '../types/rarity';
import { Attribute } from '../types/validator';
import { NFTMetadata } from '../types/nft-metadata';
Expand All @@ -28,12 +28,13 @@ import { MetadataObject } from '../types/csv';

/**
*
* @param {string} dir Absolute path to folder with metadata files for rarity calculation
* @param {string} relativePath Relative path to folder with metadata files for rarity calculation
* @return {RarityResult[]} Array of objects with rarity information for each NFT
*/
const calculateRarity = (dir: string): RarityResult[] => {
const filenames = getJSONFilesForDir(dir);
const files = readFiles(dir, filenames);
const calculateRarity = (relativePath: string): RarityResult[] => {
const absolutePath = path.resolve(relativePath)
const filenames = getJSONFilesForPath(absolutePath);
const files = readFiles(absolutePath, filenames);

const attributesMap = getAttributeMap(files);

Expand Down

0 comments on commit 4d3d11e

Please sign in to comment.