Adopt `options` object for optional parameters
See original GitHub issueFormalizing @DenisCarriere’s elegant proposal, which I really like. 😄
I would actually include all the optional parameters in the options
object, making it more consistent throughout the library.
/**
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units="kilometers"] can be degrees, radians, miles, kilometers, ...
* @param {number} [options.steps=64] number of steps on a rounded corner
*/
module.exports = function (feature, distance, options) {
options = options || {};
var units = options.units;
var steps = options.steps;
As suggested, we might formally adopt this for new modules and refactor the existing modules in the new major release.
cc: @rowanwins
Single optional parameter (Backwards compatible with v4.0)
/**
* @param {Feature<LineString>} line input line
* @param {number} distance distance along the line
* @param {Object} [options] Optional parameters
* @param {string} [options.units="kilometers"] can be degrees, radians, miles, or kilometers
*/
export default function (line, distance, options) {
// Backwards compatible with v4.0
var units = (typeof options === 'object') ? options.units : options;
index.d.ts
interface Options {
units?: Units;
}
export default function along(line: LineString, distance: number, options?: Options): Point;
Multiple optional parameters (❗️ breaking change)
/**
* @param {Feature<LineString>} line input LineString
* @param {Object} [options] Optional parameters
* @param {number} [options.resolution=10000] time in milliseconds between points
* @param {number} [options.sharpness=0.85] a measure of how curvy the path should be between splines
*/
export default function (line, options) {
// Optional params
options = options || {};
var resolution = options.resolution || 10000;
var sharpness = options.sharpness || 0.85;
// validation
if (typeof options !== 'object') throw new Error('options must be an object');
if (typeof resolution !== 'number') throw new Error('options must be an number');
if (typeof sharpness !== 'number') throw new Error('sharpness must be an number');
index.d.ts
interface Options {
resolution?: number;
sharpness?: number;
}
export default function bezier(line: LineString, options?: Options): LineString;
Apply to the following modules:
- @turf/along (@DenisCarriere)
- @turf/bearing (@DenisCarriere)
- @turf/bezier (@DenisCarriere)
- @turf/boolean-point-on-line (@DenisCarriere)
- @turf/buffer (@DenisCarriere)
- @turf/circle (@DenisCarriere)
- @turf/clean-coords (@DenisCarriere)
- @turf/clusters-dbscan (@DenisCarriere)
- @turf/clusters-kmeans (@DenisCarriere)
- @turf/concave (@DenisCarriere)
- @turf/destination (@DenisCarriere)
- @turf/dissolve (@DenisCarriere)
- @turf/distance (@DenisCarriere)
- @turf/flip (@DenisCarriere)
- @turf/great-circle
- @turf/helpers
- @turf/hex-grid
- ~@ turf/idw~ this is going to be deprecated
- @turf/inside
- @turf/interpolate
- @turf/isobands
- @turf/isolines
- @turf/line-arc
- @turf/line-chunk
- @turf/line-distance
- @turf/line-offset
- @turf/line-overlap
- @turf/line-slice-along
- @turf/linestring-to-polygon
- @turf/meta
- @turf/nearest-point-to-line
- @turf/point-grid (@stebogit)
- @turf/point-on-line
- @turf/point-to-line-distance
- @turf/projection
- @turf/random
- @turf/rewind
- @turf/rhumb-bearing (@stebogit)
- @turf/rhumb-destination (@stebogit)
- @turf/rhumb-distance (@stebogit)
- @turf/sector
- @turf/simplify
- @turf/square-grid
- @turf/transform-rotate
- @turf/transform-scale
- @turf/transform-translate
- @turf/triangle-grid
- @turf/truncate
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Why should Java 8's Optional not be used in arguments
When a method can accept optional parameters, it's preferable to adopt the well-proven approach and design such case using method overloading.
Read more >How to use optional parameters in JSON.stringify and JSON ...
This object provides two methods for generating and parsing JSON format, the JSON.stringify() method and the JSON.parse() method. Without ...
Read more >Named Parameters using Destructured Objects
Since JavaScript doesn't natively support named parameters. Here's a solution using object. No more worrying about the specific order of inserting your ...
Read more >Optional Function Parameters With Default Values Via ...
Optional parameters are a thing of the past. Use the object spread syntax for easy parameter overriding.
Read more >Opinions on using Optional<> as parameter : r/java - Reddit
So by using an Optional parameter you have added complexity, added extra code, and added an extra null-risk, with no actual benefit.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Done
🎉 All done… now just need to review the remaining errors that were caused by the refactoring.
https://github.com/Turfjs/turf/pull/993