Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/check ordering js should succeed #14

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
15 changes: 7 additions & 8 deletions .github/workflows/check-csv-ordering.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Generate header-free version
run: tail -n +2 fate-product-list.csv > headerless.csv

- name: Generate sorted version
run: LC_ALL=en_US.UTF-8 sort headerless.csv | head -c -1 > sorted.csv

- name: Compare sorted and actual list
run: diff --color=always -u headerless.csv sorted.csv
- name: Setup nodejs
uses: actions/setup-node@v4
with:
node-version: '20.x'
- run: npm ci
- name: Check ordering
run: npm test
615 changes: 308 additions & 307 deletions fate-product-list.csv

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A list of Fate RPG products.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node scripts/check-csv-order.js"
},
"repository": {
"type": "git",
Expand All @@ -15,5 +15,8 @@
"bugs": {
"url": "https://github.com/fate-srd/fate-product-list/issues"
},
"homepage": "https://github.com/fate-srd/fate-product-list#readme"
}
"homepage": "https://github.com/fate-srd/fate-product-list#readme",
"devDependencies": {
"csv-parse": "^5.5.6"
}
}
37 changes: 37 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,43 @@ The submission will be reviewed and added to the Fate SRD. This usually happens

If you are comfortable with editing files and git/Github, you may [create a pull request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request). It will be reviewed and added to the site.

There is a Github check configured to ensure the CSV file is in the expected order. This check is implemented a small
Javascript script that runs under [Nodejs](https://nodejs.org/) version 20.x. (The code ill probably run under older versions, too.)

To check the CSV file order locally, before you push your changes to Github, you must have a current version of Nodejs
installed, then run the following commands.

```bash
npm install
npm test
```

Example output of successful test:
```
% npm test

> [email protected] test
> node scripts/check-csv-order.js

check-csv-order: started.
check-csv-order: finished, fate-product-list.csv is correctly ordered.
%
```

Example output of unsuccessful test:
```
% npm test

> [email protected] test
> node scripts/check-csv-order.js

check-csv-order: started.
Mismatch on record 222:
input: "Publisher Name: Modiphius, Product title: Mindjammer: The Mindjammer Companion"
sorted: "Publisher Name: Modiphius, Product title: Mindjammer: The Core Worlds Sourcebook"
%
```

### Submitting an issue

1. Be logged into Github.
Expand Down
65 changes: 65 additions & 0 deletions scripts/check-csv-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

const process = require('node:process');
const fs = require('node:fs/promises');

const {parse: csvParse} = require('csv-parse/sync');

const compStrings = new Intl.Collator('en-us', {sensitivity: 'base'}).compare; // , ignorePunctuation: true

/**
* Compares 2 product records by publisher then title.
*
* Used in sort function to sort.
*
* @param {"Publisher Name": string, "Product title": string} a
* @param {"Publisher Name": string, "Product title": string} b
* @returns {number}
*/
function compRecords(a, b) {
const publisherA = a["Publisher Name"];
const titleA = a["Product title"];

const publisherB = b["Publisher Name"];
const titleB = b["Product title"];

const pubCompare = compStrings(publisherA, publisherB);
if (pubCompare === 0) {
return compStrings(titleA, titleB);
}
return pubCompare;
}

/**
* Generates a string describing the record.
*
* @param {"Publisher Name": string, "Product title": string} a
* @returns {string}
*/
function recordToString(a) {
return `Publisher Name: ${a["Publisher Name"]}, Product title: ${a["Product title"]}`
}

(async (path) => {
console.log('check-csv-order: started.');

const fileContents = await fs.readFile(path, {encoding: 'utf8'});

// Parse CSV rows into object records.
const records = csvParse(fileContents, {columns: true});
// console.log('records: ', records);

// Sort records
const recordsSorted = records.toSorted(compRecords);
// console.log('recordsSorted: ', recordsSorted);

// Compare input records to sorted records, exit with process error on first difference.
for (let i = 0; i < records.length; i++) {
if (compRecords(records[i], recordsSorted[i]) !== 0) {
console.log(`Mismatch on record ${i}:\n input: "${recordToString(records[i])}"\nsorted: "${recordToString(recordsSorted[i])}"`)
process.exit(1);
}
}
console.log(`check-csv-order: finished, ${path} is correctly ordered.`);
})('fate-product-list.csv');