-
-
Notifications
You must be signed in to change notification settings - Fork 732
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
Custom stringify arrays and objects #365
Draft
leninlin
wants to merge
33
commits into
ljharb:main
Choose a base branch
from
OneSoil-Platform:custom-stringify
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
e39c235
[Tests] `Buffer.from` in node v5.0-v5.9 and v4.0-v4.4 requires a Type…
ljharb 760a670
[Dev Deps] update `eslint`, `@ljharb/eslint-config`, `evalmd`
ljharb 97154a6
[Tests] up to `node` `v12.10`, `v11.15`, `v10.16`, `v8.16`
ljharb 4019179
[Tests] add `posttest` using `npx aud` to run `npm audit` without a l…
ljharb 7f216ee
[New] `parse`/`stringify`: Pass extra key/value argument to `decoder`
df0cb44
[Dev Deps] update `eslint`
ljharb dadf9db
[Tests] `parse`: add passing `arrayFormat` tests
dreyks 670254b
v6.9.0
ljharb 698b683
[fix] `parse`: with comma true, do not split non-string values
f884e2d
[Fix] `parse`: with comma true, handle field that holds an array of a…
1f35831
[Dev Deps] update `eslint`, `@ljharb/eslint-config`
ljharb b9a032f
[meta] add `funding` field
ljharb 6151be3
[Tests] use shared travis-ci config
ljharb 7b36800
v6.9.1
ljharb 152b26c
[meta] add tidelift marketing copy
ljharb fe6384c
[Fix] `parse`: throw a TypeError instead of an Error for bad charset
ljharb 76e4570
[actions] add automatic rebasing / merge commit blocking
ljharb 72dc89f
[meta] fix indentation in package.json
ljharb 5af2bf8
[meta] ignore eclint transitive audit warning
ljharb eac5616
[Dev Deps] update `eslint`, `@ljharb/eslint-config`, `object-inspect`…
ljharb 0625c49
[Dev Deps] update `@ljharb/eslint-config`, `tape`
ljharb eecd28d
[Fix] `parse`: Fix parsing array from object with `comma` true
911efab
[Dev Deps] update `tape`, `mkdirp`, `iconv-lite`
ljharb ddc1ff9
v6.9.2
ljharb 37f6a6b
Merge changelogs from v6.7.1, v6.8.1
ljharb cd9a3cd
[Fix] parses comma delimited array while having percent-encoded comma…
bf0ea91
[Fix] proper comma parsing of URL-encoded commas
ljharb 8d1dea2
Merge changelogs from v6.7.2, v6.8.2
ljharb 511e1c9
v6.9.3
ljharb 0932740
Add stringify objects with curly braces and add possibility stringify…
leninlin e25d9aa
Revert dist files
leninlin ac71264
Add mirror to parse.js
leninlin 422d2c2
Fix example in README
leninlin 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,6 +17,18 @@ var arrayPrefixGenerators = { | |||||
} | ||||||
}; | ||||||
|
||||||
var objectPrefixGenerators = { | ||||||
brackets: function brackets(prefix, key) { | ||||||
return prefix + '[' + key + ']'; | ||||||
}, | ||||||
curly: function curly(prefix, key) { | ||||||
return prefix + '{' + key + '}'; | ||||||
}, | ||||||
dots: function dots(prefix, key) { | ||||||
return prefix + '.' + key; | ||||||
} | ||||||
}; | ||||||
|
||||||
var isArray = Array.isArray; | ||||||
var push = Array.prototype.push; | ||||||
var pushToArray = function (arr, valueOrArray) { | ||||||
|
@@ -28,6 +40,7 @@ var toISO = Date.prototype.toISOString; | |||||
var defaultFormat = formats['default']; | ||||||
var defaults = { | ||||||
addQueryPrefix: false, | ||||||
// deprecated | ||||||
allowDots: false, | ||||||
charset: 'utf-8', | ||||||
charsetSentinel: false, | ||||||
|
@@ -58,12 +71,12 @@ var stringify = function stringify( | |||||
object, | ||||||
prefix, | ||||||
generateArrayPrefix, | ||||||
generateObjectPrefix, | ||||||
strictNullHandling, | ||||||
skipNulls, | ||||||
encoder, | ||||||
filter, | ||||||
sort, | ||||||
allowDots, | ||||||
serializeDate, | ||||||
formatter, | ||||||
encodeValuesOnly, | ||||||
|
@@ -120,12 +133,12 @@ var stringify = function stringify( | |||||
obj[key], | ||||||
typeof generateArrayPrefix === 'function' ? generateArrayPrefix(prefix, key) : prefix, | ||||||
generateArrayPrefix, | ||||||
generateObjectPrefix, | ||||||
strictNullHandling, | ||||||
skipNulls, | ||||||
encoder, | ||||||
filter, | ||||||
sort, | ||||||
allowDots, | ||||||
serializeDate, | ||||||
formatter, | ||||||
encodeValuesOnly, | ||||||
|
@@ -134,14 +147,14 @@ var stringify = function stringify( | |||||
} else { | ||||||
pushToArray(values, stringify( | ||||||
obj[key], | ||||||
prefix + (allowDots ? '.' + key : '[' + key + ']'), | ||||||
generateObjectPrefix(prefix, key), | ||||||
generateArrayPrefix, | ||||||
generateObjectPrefix, | ||||||
strictNullHandling, | ||||||
skipNulls, | ||||||
encoder, | ||||||
filter, | ||||||
sort, | ||||||
allowDots, | ||||||
serializeDate, | ||||||
formatter, | ||||||
encodeValuesOnly, | ||||||
|
@@ -220,16 +233,27 @@ module.exports = function (object, opts) { | |||||
return ''; | ||||||
} | ||||||
|
||||||
var arrayFormat; | ||||||
if (opts && opts.arrayFormat in arrayPrefixGenerators) { | ||||||
arrayFormat = opts.arrayFormat; | ||||||
var generateArrayPrefix; | ||||||
if (opts && typeof opts.arrayFormat === 'function') { | ||||||
generateArrayPrefix = opts.arrayFormat; | ||||||
} else if (opts && opts.arrayFormat in arrayPrefixGenerators) { | ||||||
generateArrayPrefix = arrayPrefixGenerators[opts.arrayFormat]; | ||||||
} else if (opts && 'indices' in opts) { | ||||||
arrayFormat = opts.indices ? 'indices' : 'repeat'; | ||||||
generateArrayPrefix = arrayPrefixGenerators[opts.indices ? 'indices' : 'repeat']; | ||||||
} else { | ||||||
arrayFormat = 'indices'; | ||||||
generateArrayPrefix = arrayPrefixGenerators.indices; | ||||||
} | ||||||
|
||||||
var generateArrayPrefix = arrayPrefixGenerators[arrayFormat]; | ||||||
var generateObjectPrefix; | ||||||
if (opts && typeof opts.objectFormat === 'function') { | ||||||
generateObjectPrefix = opts.objectFormat; | ||||||
} else if (opts && opts.objectFormat in objectPrefixGenerators) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
generateObjectPrefix = objectPrefixGenerators[opts.objectFormat]; | ||||||
} else if (opts && opts.allowDots) { | ||||||
generateObjectPrefix = objectPrefixGenerators.dots; | ||||||
} else { | ||||||
generateObjectPrefix = objectPrefixGenerators.brackets; | ||||||
} | ||||||
Comment on lines
+252
to
+256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems like it should be handled by the option normalization and the default? |
||||||
|
||||||
if (!objKeys) { | ||||||
objKeys = Object.keys(obj); | ||||||
|
@@ -249,12 +273,12 @@ module.exports = function (object, opts) { | |||||
obj[key], | ||||||
key, | ||||||
generateArrayPrefix, | ||||||
generateObjectPrefix, | ||||||
options.strictNullHandling, | ||||||
options.skipNulls, | ||||||
options.encode ? options.encoder : null, | ||||||
options.filter, | ||||||
options.sort, | ||||||
options.allowDots, | ||||||
options.serializeDate, | ||||||
options.formatter, | ||||||
options.encodeValuesOnly, | ||||||
|
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.
similarly here