-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chris/448-remove-old-code
- Loading branch information
Showing
6 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2024 Climate Interactive / New Venture Fund | ||
|
||
/** | ||
* Helper function that converts a Vensim variable or subscript name | ||
* into a valid C identifier as used by SDE. | ||
* TODO: Import helper function from `compile` package instead | ||
*/ | ||
function sdeNameForVensimName(name: string): string { | ||
return ( | ||
'_' + | ||
name | ||
.trim() | ||
.replace(/"/g, '_') | ||
.replace(/\s+!$/g, '!') | ||
.replace(/\s/g, '_') | ||
.replace(/,/g, '_') | ||
.replace(/-/g, '_') | ||
.replace(/\./g, '_') | ||
.replace(/\$/g, '_') | ||
.replace(/'/g, '_') | ||
.replace(/&/g, '_') | ||
.replace(/%/g, '_') | ||
.replace(/\//g, '_') | ||
.replace(/\|/g, '_') | ||
.toLowerCase() | ||
) | ||
} | ||
|
||
/** | ||
* Helper function that converts a Vensim variable name (possibly containing | ||
* subscripts) into a valid C identifier as used by SDE. | ||
* TODO: Import helper function from `compile` package instead | ||
*/ | ||
export function sdeNameForVensimVarName(varName: string): string { | ||
const m = varName.match(/([^[]+)(?:\[([^\]]+)\])?/) | ||
if (!m) { | ||
throw new Error(`Invalid Vensim name: ${varName}`) | ||
} | ||
let id = sdeNameForVensimName(m[1]) | ||
if (m[2]) { | ||
const subscripts = m[2].split(',').map(x => sdeNameForVensimName(x)) | ||
id += `[${subscripts.join('][')}]` | ||
} | ||
|
||
return id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters