“This is the 28th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
What is a warbler – js
Warbler-js is a library of JavaScript utilities.
The installation
Through the NPM
npm i --save warbler-js
Copy the code
developers
A tail prostitutes
👉 👉 nuggets
👉 👉 making
👉 👉 gitee
👉👉 personal blog
👉👉 Warblers start page
An array of
Randomize the order of array elements
grammar
const result = shuffleArr(arr)
Copy the code
parameter
arr
(Array) : an array that needs to be scrambled.
The return value
Array: shuffled Array.
The source code
const shuffleArr = (arr) = > arr.sort(() = > Math.random() - 0.5);
Copy the code
example
import { shuffleArr } from 'warbler-js'
const ages = [1.2.3.4.5.6.7.8]
const result = shuffleArr(ages)
console.log(result) //=> [1, 7, 2, 3, 8, 4, 5, 6]
Copy the code
Gets the different elements between two arrays
grammar
const result = arrDifference(arr1, arr2)
Copy the code
parameter
arr1
(Array) : Array one that needs to get different elements.arr2
(Array) : Array two that needs to get different elements.
The return value
Array: An Array of different elements between two arrays.
The source code
const arrDifference = (arr1, arr2) = > arr1.concat(arr2).filter((v, i, arr) = > arr.indexOf(v) === arr.lastIndexOf(v));
Copy the code
example
import { arrDifference } from 'warbler-js'
const arr1 = [1.2.4.5.8]
const arr2 = [2.3.5.8.9]
const result = arrDifference(arr1,arr2)
console.log(result) / / = >,4,3,9 [1]
Copy the code
Gets the same element between two arrays
grammar
const result = arrSimilarity(arr1, arr2)
Copy the code
parameter
arr1
(Array) : Need to get array one of the same elements.arr2
(Array) : Array two that needs to get the same elements.
The return value
Array: An Array of identical elements between two arrays.
The source code
const arrSimilarity = (arr1, arr2) = > arr1.filter((v) = > arr2.includes(v));
Copy the code
example
import { arrSimilarity } from 'warbler-js'
const arr1 = [1.2.4.5.8]
const arr2 = [2.3.5.8.9]
const result = arrSimilarity(arr1,arr2)
console.log(result) / / = >,5,8 [2]
Copy the code
Gets the element of array 2 that is different from array 1
grammar
const result = getDifferenceFrom(arr1, arr2)
Copy the code
parameter
arr1
(Array) : Array one that needs to get different elements.arr2
(Array) : Array two that needs to get different elements.
The return value
Array: An Array of elements that differ from Array 1.
The source code
const getDifferenceFrom = (arr1, arr2) = > {
const values = new Set(arr2);
return arr1.filter((element) = >! values.has(element)); };Copy the code
example
import { getDifferenceFrom } from 'warbler-js'
const arr1 = [1.2.4.5.8]
const arr2 = [2.3.5.8.9]
const result = getDifferenceFrom(arr1,arr2)
console.log(result) / / = > [1, 4]
Copy the code
Removes duplicate elements from an array
grammar
const result = arrWithoutDupli(arr)
Copy the code
parameter
arr
(Array) : The array to deduplicate.
The return value
Array: Returns a new Array that has been deduplicated.
The source code
const arrWithoutDupli = (arr) = > [...new Set(arr)];
Copy the code
example
import { arrWithoutDupli } from 'warbler-js'
const fruits = ["apple"."mango"."orange"."apple"."pineapple"."pineapple"."peach"."mango"]
const result = arrWithoutDupli(fruits);
console.log(result) //=> ['apple', 'mango', 'orange', 'pineapple', 'peach']
Copy the code
Removes duplicate elements from an array of objects
grammar
constResult = arrObjectWithoutDupli(arr, key)Copy the code
parameter
arr
(Array) : The array to deduplicate.key
(String) : according to the objectkey
duplicate removal
The return value
Array: Returns a new Array that has been deduplicated.
The source code
const arrObjectWithoutDupli = (arr, key) = > arr.reduce((cur, next) = > {
if(! obj[next[key]]) { obj[next[key]] = cur.push(next); }returncur; } []);Copy the code
example
import { arrObjectWithoutDupli } from 'warbler-js'
const fruits = [
{name: 'Grapes'.quantity: 2},
{name: 'Bananas'.quantity: 5},
{name: 'Apples'.quantity: 10},
{name: 'Grapes'.quantity: 4},
{name: 'Grapes'.quantity: 6},];const result = arrObjectWithoutDupli(fruits,'name');
console.log(result) / / = >
/ / /
// {name: 'Grapes', quantity: 2},
// {name: 'Bananas', quantity: 5},
// {name: 'Apples', quantity: 10},
// ];
Copy the code
Evaluates the sum of array elements
grammar
const result = getTotal(arr)
Copy the code
parameter
arr
(Array) : array to compute the sum.
The return value
Number: Returns the sum of all elements.
The source code
const getTotal = (arr) = > arr.reduce((pre, cur) = > pre + cur, 0);
Copy the code
example
import { getTotal } from 'warbler-js'
const ages = [1.3.5.7.8]
const result = getTotal(ages)
console.log(result) / / = > 24
Copy the code
Evaluates the sum of the values of an attribute in an object array
grammar
const result = getTotalBy(arr, key)
Copy the code
parameter
arr
(Array) : An array of objects that need to calculate the sum of the values of an attribute.key
(String) : Need to calculate the sumkey
.
The return value
Number: The total value of an attribute in the object array.
The source code
const getTotalBy = (arr, key) = > arr.reduce((pre, cur) = > pre + cur[key], 0);
Copy the code
example
import { getTotalBy } from 'warbler-js'
const fruits = [
{name: 'Grapes'.quantity: 2},
{name: 'Bananas'.quantity: 5},
{name: 'Apples'.quantity: 10},
{name: 'Grapes'.quantity: 4},
{name: 'Grapes'.quantity: 6},];const result = getTotalBy(fruits,'quantity')
console.log(result) / / = > 27
Copy the code
Finds an object in an array based on its key value
grammar
const result = findObjectInArray(arr, key, value)
Copy the code
parameter
arr
(Array) : The array of targets to find.key
(String) : of the object to be foundkey
。value
(String | Number) : of the object to be foundvalue
。
The return value
Obj: Returns the found object.
The source code
const findObjectInArray = (arr, key, value) = > arr.find((obj) = > obj[key] === value);
Copy the code
example
import { findObjectInArray } from 'warbler-js'
let fruits = [
{name: 'Bananas'.quantity: 5},
{name: 'Apples'.quantity: 10},
{name: 'Grapes'.quantity: 2}];let result = findObjectInArray(fruits,'name'.'Apples');
console.log(result) //=> {name: 'Apples', quantity: 10}
Copy the code
Count the number of occurrences of elements in an array
grammar
const result = countFrequency(arr, value)
Copy the code
parameter
arr
(Array) : an array that needs to count occurrences of elements.value
(String | Number) : The element that needs to count occurrences.
The return value
Number: Number of occurrences of the target element.
The source code
const countFrequency = (arr, value) = > arr.reduce((pre, cur) = > (cur === value ? pre + 1 : pre + 0), 0);
Copy the code
example
import { countFrequency } from 'warbler-js'
const fruits = ['apple'.'mango'.'orange'.'apple'.'pineapple'.'pineapple'.'peach'.'mango'.'apple'];
const result = countFrequency(fruits,'apple')
console.log(result) / / = > 3
Copy the code
Count the number of occurrences of an attribute value in an array of objects
grammar
const result = countObjFrequency(arr, key, value)
Copy the code
parameter
arr
(Array) : an array that needs to count the number of occurrences of attribute values.key
(String) : Need to count the number of occurrenceskey
。value
(String | Number) : Need to count the number of occurrencesvalue
。
The return value
Number: indicates the Number of occurrences of the target attribute value.
The source code
const countObjFrequency = (arr, key, value) = > arr.reduce((pre, cur) = > (cur[key] === value ? pre + 1 : pre + 0), 0);
Copy the code
example
import { countObjFrequency } from 'warbler-js'
const fruits = [
{ name: 'Bananas'.quantity: 5 },
{ name: 'Apples'.quantity: 10 },
{ name: 'Bananas'.quantity: 5 },
{ name: 'Bananas'.quantity: 5 },
{ name: 'Grapes'.quantity: 2 },
{ name: 'Bananas'.quantity: 5},];const result = countObjFrequency(fruits, 'name'.'Bananas');
console.log(result) / / = > 4
Copy the code
Filters unique values in an array
grammar
const result = filterUnique(arr)
Copy the code
parameter
arr
(Array) : The array to filter.
The return value
Array: indicates the filtered Array.
The source code
const filterUnique = (arr) = > arr.filter((i) = >arr.indexOf(i) ! == arr.lastIndexOf(i));Copy the code
example
import { filterUnique } from 'warbler-js'
const arr = [1.2.2.3.3.4.5.5.6]
const result = filterUnique(arr)
console.log(result) / / = >,2,3,3,5,5 [2]
Copy the code
Filter non-unique values in an array
grammar
const result = filterNoUnique(arr)
Copy the code
parameter
arr
(Array) : The array to filter.
The return value
Array: indicates the filtered Array.
The source code
const filterNoUnique = (arr) = > arr.filter((i) = > arr.indexOf(i) === arr.lastIndexOf(i));
Copy the code
example
import { filterNoUnique } from 'warbler-js'
const arr = [1.2.2.3.3.4.5.5.6]
const result = filterNoUnique(arr)
console.log(result) / / = > minus [1]
Copy the code
Evaluates the average value of an array of specified keys
grammar
const result = averageBy(arr, key)
Copy the code
parameter
arr
(Array) : An array that needs to be averaged.key
(String) : Need to calculate the average valuekey
.
The return value
Number: specifies the average value of keys.
The source code
const averageBy = (arr, key) = > arr.reduce((pre, cur) = > pre + cur[key], 0) / arr.length;
Copy the code
example
import { averageBy } from 'warbler-js'
const fruits = [
{name: 'Grapes'.quantity: 2},
{name: 'Bananas'.quantity: 5},
{name: 'Apples'.quantity: 10},
{name: 'Grapes'.quantity: 4},
{name: 'Grapes'.quantity: 6},];const result = averageBy(fruits,'quantity')
console.log(result) / / = > 5.4
Copy the code
Gets the smallest element of the specified number in an array
grammar
const result = minArray(arr, n)
Copy the code
parameter
arr
(Array) : The array that needs to get the smallest element.n
(Number) : The number of minimum elements needed to get.
The return value
Array: Specifies the minimum number of elements in an Array.
The source code
const minArray = (arr, n = 1) = > [...arr].sort((a, b) = > a - b).slice(0, n);
Copy the code
example
import { minArray } from 'warbler-js'
const ages = [10.2.5.8.100.500.3.30.9];
const result1 = minArray(ages)
const result2 = minArray(ages,3)
console.log(result1) / / = > [2]
console.log(result2) / / = > [2, 3, 5]
Copy the code
Gets the largest element of the specified number in an array
grammar
const result = maxArray(arr, n)
Copy the code
parameter
arr
(Array) : The array that requires the largest element.n
(Number) : The maximum number of elements needed to get.
The return value
Array: An Array of the maximum number of elements specified.
The source code
const maxArray = (arr, n = 1) = > [...arr].sort((a, b) = > b - a).slice(0, n);
Copy the code
example
import { maxArray } from 'warbler-js'
const ages = [10.2.5.8.100.500.3.30.9];
const result1 = maxArray(ages)
const result2 = maxArray(ages,3)
console.log(result1) / / = > [500]
console.log(result2) / / = > [500, 100, 30]
Copy the code
Check whether all elements meet requirements
grammar
const result = isAllPass(arr, fn)
Copy the code
parameter
arr
(Array) : The array to check.fn
(Function) : validates the conditional callback function.
The return value
Boolean: true all elements pass the check. False Any elements fail the check.
The source code
const isAllPass = (arr, fn) = > arr.every(fn);
Copy the code
example
import { isAllPass } from 'warbler-js'
const ages = [30.24.28.32];
const result1 = isAllPass(ages, (item) = > item < 40);
const result2 = isAllPass(ages, (item) = > item > 30);
console.log(result1); //=> true
console.log(result2); //=> false
Copy the code
object
string
Converts the first character of a string to lowercase
grammar
const result = toLowerFirstLetter(str)
Copy the code
parameter
[first, ...rest]
(String) : String that needs to convert the first letter to lowercase.
The return value
String: A String that converts the first letter to lowercase.
The source code
const toLowerFirstLetter = ([first, ...rest]) = > first.toLowerCase() + rest.join(' ');
Copy the code
example
import { toLowerFirstLetter } from 'warbler-js'
const initStr = 'Hello,world'
const finallyStr = toLowerFirstLetter(initStr)
console.log(finallyStr) //=> 'hello,world'
Copy the code
Converts the first letter of a string to uppercase
grammar
const result = toUpperFirstLetter(str)
Copy the code
parameter
[first, ...rest]
(String) : string that needs to convert the first letter to uppercase.
The return value
String: A String that converts the first letter to uppercase.
The source code
const toUpperFirstLetter = ([first, ...rest]) = > first.toUpperCase() + rest.join(' ');
Copy the code
example
import { toUpperFirstLetter } from 'warbler-js'
const initStr = 'hello,world'
const finallyStr = toUpperFirstLetter(initStr)
console.log(finallyStr) //=> 'Hello,world
Copy the code
Inverted string
grammar
const result = reverseString(str)
Copy the code
parameter
str
(String) : string to be reversed.
The return value
String: Returns the inverted String.
The source code
const reverseString = (str) = > str.split(' ').reverse().join(' ');
Copy the code
example
import { reverseString } from 'warbler-js'
const initStr = 'hello,world'
const finallyStr = reverseString(initStr)
console.log(finallyStr) //=> dlrow,olleh
Copy the code
Capitalize the first letter of each word in the string
grammar
const result = capitalizeWord(str)
Copy the code
parameter
str
(String) : a string that needs to capitalize the first letter of each word.
The return value
String: A String in which the first letter of each word is capitalized.
The source code
const capitalizeWord = (string) = > string.replace(/\b[a-z]/g.(letter) = > letter.toUpperCase());
Copy the code
example
import { capitalizeWord } from 'warbler-js'
const initStr = 'hello world , my name is warbler ! '
const finallyStr = capitalizeWord(initStr)
console.log(finallyStr) //=> Hello World , My Name Is Warbler !
Copy the code
Puts strings in alphabetical order
grammar
const result = alphabetical(str)
Copy the code
parameter
str
(String) : A string that needs to be arranged alphabetically.
The return value
String: a String in alphabetical order.
The source code
const alphabetical = (str) = > str.split(' ').sort((a, b) = > a.localeCompare(b)).join(' ');
Copy the code
example
import { alphabetical } from 'warbler-js'
const initStr = 'adaskjdhaskjdhjkgrjheg'
const result = alphabetical(initStr)
console.log(result) //=> aaadddegghhhjjjjkkkrss
Copy the code
digital
Determine whether a number is divisible
grammar
const result1 = isDivisible(dividend, divisor)
Copy the code
parameter
dividend
(Number) : dividend.divisor
(Number) : divisor.
The return value
Boolean: True is divisible. False is not divisible.
The source code
const isDivisible = (dividend, divisor) = > dividend % divisor === 0;
Copy the code
example
import { isDivisible } from 'warbler-js'
const result1 = isDivisible(6.3)
const result2 = isDivisible(5.3)
console.log(result1) //=> true
console.log(result2) //=> false
Copy the code
Generate a range of random numbers
grammar
const result1 = randomInRange(min, max)
Copy the code
parameter
min
(Number) : minimum range for generating random numbers.max
(Number) : Maximum range for generating random numbers.
The return value
Number: a range of random numbers.
The source code
const randomInRange = (min, max) = > Math.random() * (max - min) + min;
Copy the code
example
import { randomInRange } from 'warbler-js'
const result = randomInRange(3.9)
console.log(result) / / = > 7.807953357253535
Copy the code
Generates random integers in range
grammar
const result1 = randomIntegerInRange(min, max)
Copy the code
parameter
min
(Number) : Generates the minimum range of random integers.max
(Number) : Generates the maximum range of random integers.
The return value
Number: a random integer in a range.
The source code
const randomIntegerInRange = (min, max) = > Math.floor(Math.random() * (max - min + 1)) + min;
Copy the code
example
import { randomIntegerInRange } from 'warbler-js'
const result = randomIntegerInRange(3.9)
console.log(result) / / = > 7
Copy the code
averaging
grammar
constresult = getAverage(... args)Copy the code
parameter
. args
(Number) : Target number, unlimited number of parameters.
The return value
Number: indicates the average of all parameters.
The source code
const getAverage = (. args) = > args.reduce((a, b) = > a + b) / args.length;
Copy the code
example
import { getAverage } from 'warbler-js'
const result1 = getAverage(1.2.3.4)
const result2 = getAverage(1.2.3.4.5.6.7.8)
console.log(result1) / / = > 2.5
console.log(result2) / / = > 4.5
Copy the code
Judge parity
grammar
const result = isEvenNumber(num)
Copy the code
parameter
num
(Number) : The odd and even numbers need to be judged.
The return value
Boolean: true even, false odd.
The source code
const isEvenNumber = (num) = > num % 2= = =0;
Copy the code
example
import { isEvenNumber } from 'warbler-js'
const result1 = isEvenNumber(4)
const result2 = isEvenNumber(5)
console.log(result1) //=> true
console.log(result2) //=> false
Copy the code
function
Boolean value
Get a random Boolean value
grammar
const result = randomBoolean()
Copy the code
parameter
null
The return value
Boolean: A random Boolean value.
The source code
const randomBoolean = () = > Math.random() >= 0.5;
Copy the code
example
import { randomBoolean } from 'warbler-js'
const result = randomBoolean()
console.log(result) //=> true
Copy the code
The date of
Gets the day of the year in which the specified date is
grammar
const result = dayOfYear(date)
Copy the code
parameter
date
(String) : specifies the datenew Date()
And supportyyyy-mm-dd
Format, not to be obtained by default.
The return value
Number: specifies the day of the year in which the date is specified.
The source code
const dayOfYear = (date) = > {
const myData = date ? new Date(typeof date === 'string' && date.includes(The '-')? date.replace(/-/g.'/') : date) : new Date(a);return Math.floor((myData - new Date(myData.getFullYear(), 0.0)) / 1000 / 60 / 60 / 24);
};
Copy the code
example
import { dayOfYear } from 'warbler-js'
const result1 = dayOfYear()
const result2 = dayOfYear("2021,9,15")
const result3 = dayOfYear("2021-9-16")
console.log(result1) / / = > 257
console.log(result2) / / = > 258
console.log(result3) / / = > 259
Copy the code
Gets the difference between two dates
grammar
const result = getDayDiff(date1, date2, unit)
Copy the code
parameter
date1
(String) : specifies the date 1new Date()
And supportyyyy-mm-dd
Format.date2
(String) : specifies the date 2new Date()
And supportyyyy-mm-dd
Format.unit
(String) : Sets the unit of difference. The following values are supported.
day | hour | minute | second | ms |
---|---|---|---|---|
day | hours | minutes | seconds | ms |
The return value
Number: The difference between two dates.
The source code
const getDayDiff = (date1, date2, unit) = > {
const myDate1 = typeof date1 === 'string' && date1.includes(The '-')? date1.replace(/-/g.'/') : date1;
const myDate2 = typeof date2 === 'string' && date2.includes(The '-')? date2.replace(/-/g.'/') : date2;
const map = {
day: 1000 * 60 * 60 * 24.hour: 1000 * 60 * 60.minute: 1000 * 60.second: 1000.ms: 1};return Math.abs((new Date(myDate2) - new Date(myDate1)) / (map[unit]));
};
Copy the code
example
import { getDayDiff } from 'warbler-js'
// In days
const result1 = getDayDiff("2021,9,15".'2021,9,16'.'day')
// In hours
const result2 = getDayDiff("2021,9,15".'2021,9,16'.'hour')
// In minutes
const result3 = getDayDiff("2021,9,15".'2021,9,16'.'minute')
// In seconds
const result4 = getDayDiff("2021,9,15".'2021,9,16'.'second')
// in milliseconds
const result5 = getDayDiff("2021,9,15".'2021,9,16'.'ms')
console.log(result1) / / = > 1
console.log(result2) / / = > 24
console.log(result3) / / = > 1440
console.log(result4) / / = > 86400
console.log(result5) / / = > 86400000
Copy the code
Determine if the specified date is today
grammar
const result = isToday(date)
Copy the code
parameter
date
(String) : specifies the datenew Date()
And supportyyyy-mm-dd
Format, not to be obtained by default.
The return value
Boolean: true is today, false is not today.
The source code
const isToday = (date) = > {
// The current date
const curDate = new Date(a);// Specify a date
const tarData = date ? new Date(typeof date === 'string' && date.includes(The '-')? date.replace(/-/g.'/') : date) : new Date(a);return ['getFullYear'.'getMonth'.'getDate'].every((i) = > curDate[i]() === tarData[i]());
};
Copy the code
example
import { isToday } from 'warbler-js'
// Test date is 2021-09-26
const result1 = isToday(new Date())
const result2 = isToday("1998-03-09")
console.log(result1) //=> true
console.log(result2) //=> false
Copy the code
Check whether the specified date is n days later
grammar
const result = isTomorrow(date, n)
Copy the code
parameter
date
(String) : specifies the datenew Date()
And supportyyyy-mm-dd
Format, not to be obtained by default.n
(Number) :n
After days, the default value is no1
That is, tomorrow.
The return value
Boolean: true is n days, false is n days.
The source code
const isTomorrow = (date, n = 1) = > {
const curDate = new Date(a);// The current date
curDate.setDate(curDate.getDate() + n); // The current date plus one day
// Specify a date
const tarData = date ? new Date(typeof date === 'string' && date.includes(The '-')? date.replace(/-/g.'/') : date) : new Date(a);return ['getFullYear'.'getMonth'.'getDate'].every((i) = > curDate[i]() === tarData[i]());
};
Copy the code
example
import { isTomorrow } from 'warbler-js'
// Test date is 2021-09-26
const result1 = isTomorrow(new Date())
const result2 = isTomorrow("2021-09-27".1)
const result3 = isTomorrow("2021-09-27".2)
const result4 = isTomorrow("2021-09-28".2)
console.log(result1) //=> false
console.log(result2) //=> true
console.log(result3) //=> false
console.log(result4) //=> true
Copy the code
Check whether the specified date is n days ago
grammar
const result = isYesterday(date, n)
Copy the code
parameter
date
(String) : specifies the datenew Date()
And supportyyyy-mm-dd
Format, not to be obtained by default.n
(Number) :n
Days ago: No Default value1
Yesterday, that is.
The return value
Boolean: true indicates n days ago. False indicates n days ago.
The source code
const isYesterday = (date, n = 1) = > {
const curDate = new Date(a);// The current date
curDate.setDate(curDate.getDate() - n); // Current date minus n days
// Specify a date
const tarData = date ? new Date(typeof date === 'string' && date.includes(The '-')? date.replace(/-/g.'/') : date) : new Date(a);return ['getFullYear'.'getMonth'.'getDate'].every((i) = > curDate[i]() === tarData[i]());
};
Copy the code
example
import { isYesterday } from 'warbler-js'
// Test date is 2021-09-26
const result1 = isYesterday(new Date())
const result2 = isYesterday("2021-09-25".1)
const result3 = isYesterday("2021-09-25".2)
const result4 = isYesterday("2021-09-24".2)
console.log(result1) //=> false
console.log(result2) //=> true
console.log(result3) //=> false
console.log(result4) //=> true
Copy the code
Commonly used method
Asynchronously loading script scripts
grammar
loadScript('https://cdn.bootcdn.net/ajax/libs/vConsole/3.7.0/vconsole.min.js')
Copy the code
parameter
src
(String) : address of the script to load asynchronously.
The return value
- Promise: a Promise
The source code
const loadScript = (src) = > new Promise((resolve) = > {
const script = document.createElement('script');
script.type = 'text/javascript';
script.onload = () = > {
resolve();
};
script.src = src;
document.body.appendChild(script);
});
Copy the code
example
import { loadScript } from 'warbler-js';
const loadVConsole = async() = > {await loadScript('https://cdn.bootcdn.net/ajax/libs/vConsole/3.7.0/vconsole.min.js');
};
loadVConsole()
Copy the code
Simulation of delay
grammar
imitateDelay(timeout)
Copy the code
parameter
timeout
(Number) : Delay time in milliseconds (ms
).
The return value
Promise: a Promise.
The source code
const imitateDelay = (timeout) = > new Promise(
(resolve) = > {
const timeoutHandle = setTimeout(() = > {
clearTimeout(timeoutHandle); resolve(); }, timeout); });Copy the code
example
import { imitateDelay } from 'warbler-js';
async function test() {
console.log('The first log');
await imitateDelay(1000);
console.log('The second log with 1000 ms delay'); // => The second log with 1000 ms delay is output after 1000 milliseconds
}
test();
Copy the code
Gets the URL of the current page
grammar
const result = getCurrentUrl()
Copy the code
parameter
null
The return value
String: the URL of the current page.
The source code
const getCurrentUrl = () = > window.location.href;
Copy the code
example
import { getCurrentUrl } from 'warbler-js';
const result = getCurrentUrl()
console.log(result) //=>http://warblerjs.duwanyu.com
Copy the code
Scroll to the top of the page
grammar
scrollToTop()
Copy the code
parameter
null
The return value
void
The source code
const scrollToTop = () = > {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8); }};Copy the code
example
import { scrollToTop } from 'warbler-js';
scrollToTop()
Copy the code