-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement simply array polyfills (fixes #328)
- Loading branch information
Showing
9 changed files
with
133 additions
and
45 deletions.
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
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,45 @@ | ||
/* | ||
* To reduce build size, this file provides simple polyfills without | ||
* overly excessive type checking and without modifying | ||
* the global Array.prototype | ||
* The polyfills are automatically removed in the commonjs build | ||
* Also, only files in client/ & shared/ should use these functions | ||
* files in server/ still use normal js function | ||
*/ | ||
|
||
// this const is replaced by rollup to true for umd builds | ||
// which means the polyfills are removed for other build formats | ||
const polyfill = process.env.NODE_ENV === 'test' | ||
|
||
export function findIndex(array, predicate) { | ||
if (polyfill && !Array.prototype.findIndex) { | ||
// idx needs to be a Number, for..in returns string | ||
for (let idx = 0; idx < array.length; idx++) { | ||
if (predicate.call(arguments[2], array[idx], idx, array)) { | ||
return idx | ||
} | ||
} | ||
return -1 | ||
} | ||
return array.findIndex(predicate, arguments[2]) | ||
} | ||
|
||
export function toArray(arg) { | ||
if (polyfill && !Array.from) { | ||
return Array.prototype.slice.call(arg) | ||
} | ||
return Array.from(arg) | ||
} | ||
|
||
export function includes(array, value) { | ||
if (polyfill && !Array.prototype.includes) { | ||
for (const idx in array) { | ||
if (array[idx] === value) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
return array.includes(value) | ||
} |
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,63 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import { findIndex, includes, toArray } from '../../src/utils/array' | ||
import { ensureIsArray } from '../../src/utils/ensure' | ||
import { hasGlobalWindowFn } from '../../src/utils/window' | ||
|
||
describe('shared', () => { | ||
afterEach(() => jest.restoreAllMocks()) | ||
|
||
test('ensureIsArray ensures var is array', () => { | ||
let a = { p: 1 } | ||
expect(ensureIsArray(a)).toEqual([]) | ||
|
||
a = 1 | ||
expect(ensureIsArray(a)).toEqual([]) | ||
|
||
a = [1] | ||
expect(ensureIsArray(a)).toBe(a) | ||
}) | ||
|
||
test('ensureIsArray ensures obj prop is array', () => { | ||
const a = { p: 1 } | ||
expect(ensureIsArray(a, 'p')).toEqual({ p: [] }) | ||
}) | ||
|
||
test('no error when window is not defined', () => { | ||
expect(hasGlobalWindowFn()).toBe(false) | ||
}) | ||
|
||
/* eslint-disable no-extend-native */ | ||
test('findIndex polyfill', () => { | ||
const _findIndex = Array.prototype.findIndex | ||
Array.prototype.findIndex = false | ||
|
||
const arr = [1, 2, 3] | ||
expect(findIndex(arr, v => v === 2)).toBe(1) | ||
expect(findIndex(arr, v => v === 4)).toBe(-1) | ||
|
||
Array.prototype.findIndex = _findIndex | ||
}) | ||
|
||
test('includes polyfill', () => { | ||
const _includes = Array.prototype.includes | ||
Array.prototype.includes = false | ||
|
||
const arr = [1, 2, 3] | ||
expect(includes(arr, 2)).toBe(true) | ||
expect(includes(arr, 4)).toBe(false) | ||
|
||
Array.prototype.includes = _includes | ||
}) | ||
|
||
test('from/toArray polyfill', () => { | ||
const _from = Array.from | ||
Array.from = false | ||
|
||
expect(toArray('foo')).toEqual(['f', 'o', 'o']) | ||
|
||
Array.from = _from | ||
}) | ||
/* eslint-enable no-extend-native */ | ||
}) |