This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
dataToNamedExports helper #29
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7fa725b
dataToNamedExports helper
guybedford 8984870
Node 0.12 support
guybedford b669268
dataToNamedExports -> dataToEsm
guybedford f0b8010
dataToEsm compact option
guybedford fdd287d
update dataToEsm implementation, provide more output options
guybedford 3dedacd
add compact:true objectShorthand:false test
guybedford 5f40964
use for loop
guybedford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,27 @@ | ||
import makeLegalIdentifier from './makeLegalIdentifier'; | ||
import tosource from 'tosource'; | ||
|
||
// convert data object into separate named exports (and default) | ||
export default function dataToNamedExports ( data, options = {} ) { | ||
const t = options.compact ? '' : 'indent' in options ? options.indent : '\t'; | ||
const _ = options.compact ? '' : ' '; | ||
const n = options.compact ? '' : '\n'; | ||
const declarationType = options.preferConst ? 'const' : 'var'; | ||
|
||
let namedExportCode = ''; | ||
const defaultExportRows = []; | ||
const dataKeys = Object.keys( data ); | ||
for (let i = 0; i < dataKeys.length; i++) { | ||
const key = dataKeys[i]; | ||
if (key === makeLegalIdentifier( key )) { | ||
if ( options.objectShorthand ) | ||
defaultExportRows.push(key); | ||
else | ||
defaultExportRows.push( `${ key }:${ _ }${ key }` ); | ||
namedExportCode += `export ${declarationType} ${key}${ _ }=${ _ }${ tosource( data[key], null, options.compact ? false : t ) };${ n }`; | ||
} else { | ||
defaultExportRows.push( `${ JSON.stringify(key) }: ${ tosource( data[key], null, options.compact ? false : t )}` ); | ||
} | ||
} | ||
return namedExportCode + `export default${ _ }{${ n }${ t }${ defaultExportRows.join(`,${ n }${ t }`) }${ n }};${ n }`; | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍