Comments (3)
export function fill<T>(array: unknown[], value: T): T[];
export function fill<T, P>(array: Array<T>, value: P, start: number): Array<T | P>;
export function fill<T, P>(array: Array<T>, value: P, start: number, end: number): Array<T | P>;
export function fill<T, P>(array: Array<T>, value: P, start = 0, end = array.length): Array<T | P> {
start = Math.max(start, 0);
end = Math.min(end, array.length);
for (let i = start; i < end; i++) {
array[i] = value;
}
return array;
}
What about implementation like this? I guess it is more simpler.
from es-toolkit.
I've given this a try:
export function fill<T>(array: any[], value: T): T[];
export function fill<T, P>(array: Array<T | P>, value: P, start?: number, end?: number): Array<T | P>;
export function fill<T, P>(array: Array<T | P>, value: P, start = 0, end?: number): Array<T | P> {
const length = array.length;
if (!length) {
return array;
}
const startIdx = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
const endIdx = end === undefined ? length : end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
for (let i = startIdx; i < endIdx; i++) {
array[i] = value;
}
return array;
}
If the above code looks good, may I proceed with enhancing the documentation and tests, and then submit a pull request?
from es-toolkit.
export function fill<T>(array: unknown[], value: T): T[]; export function fill<T, P>(array: Array<T>, value: P, start: number): Array<T | P>; export function fill<T, P>(array: Array<T>, value: P, start: number, end: number): Array<T | P>; export function fill<T, P>(array: Array<T>, value: P, start = 0, end = array.length): Array<T | P> { start = Math.max(start, 0); end = Math.min(end, array.length); for (let i = start; i < end; i++) { array[i] = value; } return array; }What about implementation like this? I guess it is more simpler.
Your code is excellent. I've learned something new!
from es-toolkit.
Related Issues (20)
- Enhancing `toFilled` Function to Support as const HOT 1
- orderBy keys don't support nested keys HOT 2
- throttle support leading and trailing HOT 1
- Support for `padEnd`
- Support for `forEach` HOT 1
- cloneDeep is not an export of es-toolkit HOT 3
- Check all files in ci, when `.eslintrc.js`, `.prettierrc`, `.prettierignore` files are changed
- Fix all of lint Error
- Add `concat`
- Support for `flatMapDeep` HOT 1
- feature request: how to use shuffle by key HOT 2
- Add a document for migrating from lodash HOT 1
- Inquiry about Supporting Lodash `template` Function in es-toolkit HOT 1
- Mismatch between docs and 1.13.1-latest HOT 3
- Unexpected behavior using orderBy HOT 6
- Suggest to enhance `cloneDeep` with custom clone rules HOT 1
- Add custom order function in `es-toolkit/compat` HOT 4
- toMerge() is not exported
- Support for `findIndex` HOT 2
- Support for `compat/indexOf` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from es-toolkit.