-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f71db9
commit ba5d998
Showing
9 changed files
with
121 additions
and
4 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,39 @@ | ||
-- | This module defines functions which are useful for determining if | ||
-- a given name is a legal JavaScript name according to <https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names this post>. | ||
module Data.Aeson.TypeScript.LegalName where | ||
|
||
import qualified Data.Set as Set | ||
import Language.Haskell.TH | ||
import Data.List.NonEmpty (NonEmpty (..)) | ||
import qualified Data.List.NonEmpty as NonEmpty | ||
import Data.Char | ||
import Data.Foldable | ||
|
||
-- | The return type is the illegal characters that are in the name. If the | ||
-- input has no illegal characters, then you have 'Nothing'. | ||
checkIllegalNameChars :: NonEmpty Char -> Maybe (NonEmpty Char) | ||
checkIllegalNameChars (firstChar :| restChars) = NonEmpty.nonEmpty $ | ||
let | ||
legalFirstCategories = | ||
Set.fromList | ||
[ UppercaseLetter | ||
, LowercaseLetter | ||
, TitlecaseLetter | ||
, ModifierLetter | ||
, OtherLetter | ||
, LetterNumber | ||
] | ||
legalRestCategories = | ||
Set.fromList | ||
[ NonSpacingMark | ||
, SpacingCombiningMark | ||
, DecimalNumber | ||
, ConnectorPunctuation | ||
] | ||
`Set.union` legalFirstCategories | ||
isIllegalFirstChar c = not $ | ||
c `elem` ['$', '_'] || generalCategory c `Set.member` legalFirstCategories | ||
isIllegalRestChar c = not $ | ||
generalCategory c `Set.member` legalRestCategories | ||
in | ||
filter isIllegalFirstChar [firstChar] <> filter isIllegalRestChar restChars |
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,23 @@ | ||
module Data.Aeson.TypeScript.LegalNameSpec where | ||
|
||
import Test.Hspec | ||
import Data.List.NonEmpty (NonEmpty (..)) | ||
import Data.Aeson.TypeScript.LegalName | ||
|
||
tests :: Spec | ||
tests = describe "Data.Aeson.TypeScript.LegalName" $ do | ||
describe "checkIllegalNameChars" $ do | ||
describe "legal Haskell names" $ do | ||
it "allows an uppercase letter" $ do | ||
checkIllegalNameChars ('A' :| []) | ||
`shouldBe` Nothing | ||
it "allows an underscore" $ do | ||
checkIllegalNameChars ('_' :| "asdf") | ||
`shouldBe` Nothing | ||
it "reports that ' is illegal" $ do | ||
checkIllegalNameChars ('F' :| "oo'") | ||
`shouldBe` Just ('\'' :| []) | ||
describe "illegal Haskell names" $ do | ||
it "allows a $" $ do | ||
checkIllegalNameChars ('$' :| "asdf") | ||
`shouldBe` Nothing |
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