rusackas commented on code in PR #24444: URL: https://github.com/apache/superset/pull/24444#discussion_r3242875330
########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import NumberFormatter from '../NumberFormatter'; + +export default function createLengthFormatter(config: { + description?: string; + id?: string; + label?: string; + convertType?: string; +}) { + const { description, id, label, convertType } = config; + + return new NumberFormatter({ + description, + formatFunc: value => { + if (convertType === 'm => km') { + return `${(value / 1000).toFixed(2).toString()}KM`; + } + if (convertType === 'cm => km') { + return `${(value / 100_000).toFixed(2).toString()}KM`; + } + if (convertType === 'cm => m') { + return `${(value / 100).toFixed(2).toString()}M`; + } + return value.toString(); + }, + id: id ?? 'length_format', + label: label ?? `Length formatter`, + }); +} Review Comment: Applied in 2ce4aefd68: - Switched to lowercase SI-correct units (`km`, `m`) - Dropped the redundant `.toString()` after `.toFixed(2)` (`toFixed` already returns a string) - Also typed `convertType` as a string-literal union so future callers get compile-time safety The return type stays inferred to match the sibling `createMemoryFormatter` factory's style. ########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import NumberFormatter from '../NumberFormatter'; + +export default function createLengthFormatter(config: { + description?: string; + id?: string; + label?: string; + convertType?: string; +}) { + const { description, id, label, convertType } = config; + + return new NumberFormatter({ + description, + formatFunc: value => { + if (convertType === 'm => km') { + return `${(value / 1000).toFixed(2).toString()}KM`; + } + if (convertType === 'cm => km') { + return `${(value / 100_000).toFixed(2).toString()}KM`; + } + if (convertType === 'cm => m') { + return `${(value / 100).toFixed(2).toString()}M`; Review Comment: Applied in 2ce4aefd68 — switched to lowercase SI units (`km`/`m`) and removed the redundant `.toString()` calls. ########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import NumberFormatter from '../NumberFormatter'; + +export default function createLengthFormatter(config: { + description?: string; + id?: string; + label?: string; + convertType?: string; +}) { Review Comment: Applied in 2ce4aefd68 — `config` now defaults to `{}` (matching the `createMemoryFormatter` factory pattern) and `convertType` is typed as a string-literal union `'m => km' | 'cm => km' | 'cm => m'` for compile-time safety. ########## superset-frontend/packages/superset-ui-chart-controls/src/utils/D3Formatting.ts: ########## @@ -68,6 +68,9 @@ export const D3_FORMAT_OPTIONS: [string, string][] = [ 'MEMORY_TRANSFER_RATE_BINARY', t('Memory transfer rate in bytes - binary (1024B => 1KiB/s)'), ], + ['LENGTH', t('Length in m (12345m => 12.34km)')], + ['LENGTH_CM_KM', t('Length in cm (12345678cm => 123.45km)')], Review Comment: Good catch — verified in node: ``` (12345/1000).toFixed(2) === '12.35' // label said 12.34 (12345678/100000).toFixed(2) === '123.46' // label said 123.45 (12345/100).toFixed(2) === '123.45' // label was correct ``` Fixed the two off-by-one rounding examples in D3Formatting.ts in 2ce4aefd68. ########## superset-frontend/packages/superset-ui-core/src/number-format/factories/createLengthFormatter.ts: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import NumberFormatter from '../NumberFormatter'; + +export default function createLengthFormatter(config: { + description?: string; + id?: string; + label?: string; + convertType?: string; +}) { + const { description, id, label, convertType } = config; + + return new NumberFormatter({ + description, + formatFunc: value => { + if (convertType === 'm => km') { + return `${(value / 1000).toFixed(2).toString()}KM`; + } + if (convertType === 'cm => km') { + return `${(value / 100_000).toFixed(2).toString()}KM`; + } + if (convertType === 'cm => m') { + return `${(value / 100).toFixed(2).toString()}M`; + } Review Comment: Applied in 2ce4aefd68 — units are now lowercase `km`/`m` per SI convention. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
