Skip to content

Commit

Permalink
feat(chemical-elements): rewrite in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Dec 3, 2024
1 parent b6cc830 commit f663e61
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/atom-sorter/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"extends": ["../../tsconfig.base.json", "../../tsconfig.strict.json"],
"include": ["src/**/*"],
"exclude": ["**/__tests__"],
"compilerOptions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFileSync, writeFileSync } from 'node:fs';

import Papa from 'papaparse';
import { format } from 'prettier';

let names = Papa.parse(
readFileSync(new URL('names.tsv', import.meta.url), 'utf8'),
Expand Down Expand Up @@ -33,8 +34,18 @@ for (let i = 0; i < elementsAndIsotopes.length; i++) {
}

writeFileSync(
new URL('../src/elementsAndIsotopes.js', import.meta.url),
`export const elementsAndIsotopes=${JSON.stringify(elementsAndIsotopes)}`,
new URL('../src/elementsAndIsotopes.ts', import.meta.url),
await format(
`import type { ElementAndIsotopes } from './types.js';\n\nexport const elementsAndIsotopes: ElementAndIsotopes[] = ${JSON.stringify(elementsAndIsotopes)};`,
{
filepath: 'elementsAndIsotopes.ts',
arrowParens: 'always',
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: 'all',
},
),
'utf8',
);

Expand Down
1 change: 0 additions & 1 deletion packages/chemical-elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "2.1.2",
"description": "JSON containing information about chemical elements and isotopes",
"main": "lib/src/index.js",
"module": "src/index.js",
"files": [
"src",
"lib"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ test('stableIsotopesObject', () => {
name: 'Carbon',
mass: 13.00335483507,
symbol: 'C',
mostAbundant: false,
});
});
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { elementsAndIsotopes } from './elementsAndIsotopes.js';
import type { Element } from './types.js';

export const elements = elementsAndIsotopes.map((element) => ({
export const elements: Element[] = elementsAndIsotopes.map((element) => ({
number: element.number,
symbol: element.symbol,
mass: element.mass,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const elementsAndIsotopes = [
import type { ElementAndIsotopes } from './types.js';

export const elementsAndIsotopes: ElementAndIsotopes[] = [
{
number: 1,
isotopes: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { elementsAndIsotopes } from './elementsAndIsotopes.js';
import type { ElementAndIsotopes } from './types.js';

export const elementsAndIsotopesObject = {};
export const elementsAndIsotopesObject: Record<
string,
ElementAndIsotopes | undefined
> = {};
for (const element of elementsAndIsotopes) {
elementsAndIsotopesObject[element.symbol] = element;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import { elementsAndIsotopes } from './elementsAndIsotopes.js';
export const elementsAndStableIsotopes = structuredClone(elementsAndIsotopes);

for (const element of elementsAndStableIsotopes) {
element.isotopes = element.isotopes.filter((i) => i.abundance > 0);
element.isotopes = element.isotopes.filter((i) => {
return typeof i.abundance === 'number' && i.abundance > 0;
});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { elementsAndStableIsotopes } from './elementsAndStableIsotopes.js';
import type { ElementAndIsotopes } from './types.js';

export const elementsAndStableIsotopesObject = {};
export const elementsAndStableIsotopesObject: Record<
string,
ElementAndIsotopes | undefined
> = {};
for (const element of elementsAndStableIsotopes) {
elementsAndStableIsotopesObject[element.symbol] = element;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { elements } from './elements.js';
import type { Element } from './types.js';

export const elementsObject = {};
export const elementsObject: Record<string, Element | undefined> = {};
for (const element of elements) {
elementsObject[element.symbol] = element;
}
11 changes: 0 additions & 11 deletions packages/chemical-elements/src/index.js

This file was deleted.

11 changes: 11 additions & 0 deletions packages/chemical-elements/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export * from './constants.js';
export * from './elements.js';
export * from './elementsAndIsotopes.js';
export * from './elementsAndIsotopesObject';
export * from './elementsAndStableIsotopes';
export * from './elementsAndStableIsotopesObject';
export * from './elementsObject';
export * from './isotopesObject';
export * from './stableIsotopesObject';
export * from './types.js';
export * from './unsaturationsObject';
12 changes: 0 additions & 12 deletions packages/chemical-elements/src/isotopesObject.js

This file was deleted.

18 changes: 18 additions & 0 deletions packages/chemical-elements/src/isotopesObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { elementsAndIsotopesObject } from './elementsAndIsotopesObject.js';

interface Isotope {
abundance: number | undefined;
mass: number;
}

export const isotopesObject: Record<string, Isotope | undefined> = {};

for (const [symbol, element] of Object.entries(elementsAndIsotopesObject)) {
if (!element) continue;
for (const isotope of element.isotopes) {
isotopesObject[`${isotope.nominal}${symbol}`] = {
abundance: isotope.abundance,
mass: isotope.mass,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { elementsAndIsotopes } from './elementsAndIsotopes.js';

export const stableIsotopesObject = {};
interface StableIsotope {
name: string;
mass: number;
symbol: string;
mostAbundant: boolean;
}

export const stableIsotopesObject: Record<string, StableIsotope | undefined> =
{};

for (const element of elementsAndIsotopes) {
let abundance = 0;
let mostAbundant = 0;
for (const isotope of element.isotopes) {
if (isotope.abundance > abundance) {
if (
typeof isotope.abundance === 'number' &&
isotope.abundance > abundance
) {
abundance = isotope.abundance;
mostAbundant = isotope.nominal;
}
Expand All @@ -18,10 +30,11 @@ for (const element of elementsAndIsotopes) {
name: element.name,
mass: isotope.mass,
symbol: element.symbol,
mostAbundant: false,
};
if (isotope.nominal === mostAbundant) {
entry.mostAbundant = true;
}
stableIsotopesObject[isotope.nominal + element.symbol] = entry;
stableIsotopesObject[`${isotope.nominal}${element.symbol}`] = entry;
}
}
17 changes: 17 additions & 0 deletions packages/chemical-elements/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface Element {
number: number;
symbol: string;
mass: number | null;
name: string;
monoisotopicMass?: number;
}

interface Isotope {
nominal: number;
mass: number;
abundance?: number;
}

export interface ElementAndIsotopes extends Element {
isotopes: Isotope[];
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const unsaturationsObject = {
export const unsaturationsObject: Record<string, number | undefined> = {
O: 0,
N: 1,
H: -1,
Expand Down
5 changes: 3 additions & 2 deletions packages/chemical-elements/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"extends": "../../tsconfig.base.json",
"extends": ["../../tsconfig.base.json", "../../tsconfig.strict.json"],
"include": ["src/**/*"],
"exclude": ["**/__tests__"],
"compilerOptions": {
"baseUrl": ".",
"outDir": "lib",
"composite": true
"composite": true,
"allowJs": false
},
"references": []
}
6 changes: 6 additions & 0 deletions tsconfig.strict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"strict": true,
"allowJs": false
}
}

0 comments on commit f663e61

Please sign in to comment.