fecmjs
v2.1.12
Published
Collection of common front-end methods to facilitate normal development. Applicable to mobile terminal (some methods come from the Internet).Modular development for easy reference.Support 'tree shaking' to reduce packing volume
Maintainers
Readme
fecmjs
Front end common method collection
English | 中文
Features
- Front-end commonly used methods collection collection , to facilitate the usual development ( part of the method from the Internet )
- Modular development, easy to reference
- support
tree shaking, in order to reduce the size of the package
Modules
Date: some methods for Date wrappingDom: some methods forDomoperationsDevice: some methods for devicesFile: Wrapping some methods aroundFileFormat: some method wrappers for formattingObject: Wrapping some methods aroundobjectArray: Some method encapsulation onarrayStorage: Wrappers forStoragemethodsUrl: Some methods aroundUrlValidate: Wrapping some methods around validationOther: Wrapping some methods around validationAnimation: aboutcss,vueusable animations
Translated with DeepL.com (free version)
Usage
- use the
npmmethod
npm i fecmjs// 1. Use all modules
import * as Fecmjs from 'fecmjs';
const flag = Fecmjs.isEmail('[email protected]');
console.log(flag); // true// 2. When you only need to use some functions, you can import some modules (recommended)
import {isEmail} from 'fecmjs';
const flag = isEmail('[email protected]');
console.log(flag); // true- Use
<script></script>to import
<script src="https://cdn.xxxx/fecmjs.min.js"></script>
<script>
var flag = fecmjs.checkEmail('[email protected]');
console.log(flag); // true
</script>Date
getCurrentTimestampGet current timestamp
Example:
import {getCurrentTimestamp} from 'femcjs'; console.log(getCurrentTimestamp()) // 1730129184412getDateByFewdaysDepending on the time of day, get the date a few days before or a few days after that time
parameter name | description | default value ------| ----| -----
fewdays| The number of days to the target day to be acquired |0time| target time |new Date().getTime()Note:
- When
fewdaysis a positive integer, get the next few days of the target date - When
fewdaysis a negative integer, get the first few days of the target date
Example:
// Get the first 30 days of 2024-10-28 import {getCurrentTimestamp} from 'femcjs'; console.log(-30, '2024-10-28'); // 2024-09-28 // Get the last 30 days of 2024-10-28 console.log(30, '2024-10-28'); // 2024-11-27- When
getDateByTimestampGet the time based on the incoming timestamp
parameter name | description | default value ------| ----| -----
timestamp| timestamp |needHMS| Whether you need to return hours, minutes and seconds |falseExample:
import {getDateByTimestamp} from 'femcjs'; console.log(getDateByTimestamp('1730129184412')); // 2024-10-28 console.log(getDateByTimestamp('1730129184412', true)); // 2024-10-28 23:26:24getTimestampByDateGet timestamps based on specific dates
parameter name | description | default value ------| ----| -----
time| Specific date. The format is the same as that passed in bynew Date()|new Date().getTime()Note:
- Returns the timestamp of the current time by default
- The form of the input parameter is the same as the
new Date()method.
Example:
import {getTimestampByDate} from 'femcjs'; console.log(getTimestampByDate()); // 1730131646512 console.log(getTimestampByDate('2024-10-29')); // 1730160000000 console.log(getTimestampByDate('2024-10-29 01:30')); // 1730136600000getTodayDateGet today's date
参数名 | 说明 | 默认值 ------| ----| -----
symbolStr| String concatenator |-Note
- Return the date of today
Example:
import {getTodayDate} from 'femcjs'; console.log(getTodayDate(':')); // 2025:11:20
Device
isAndroidIs it currently Android
isIosIs the current system ios
isIpadCurrently is not an ipad device
isIphoneCurrently not an iphone device
isMoblieIs it currently mobile
isPcIs it currently a pc
isWechatWhether the current environment is WeChat
Dom
$Returns the
Domobjectparameter name | description | default value ------| ----| -----
selectName|cssselector |Note:
if there is more than one
Dom, then return an arrayif there is only one
Dom, return a singleDom.
addClassAdds a class name to a
Domobject.parameter name | description | default value ------| ----| -----
selectName|cssselector ordomobject |nameList| The name of the class to be added |Note:
nameListcan be passed as an array when you want to add multiple class names.nameListcan be passed as a string if you want to add a single class name.
Example:
import {addClass} from 'femcjs'; addClass('#dom', ['name1', 'name2', 'nam3']);getClassGet the class name of a
Domobjectparameter name | description | default value ------| ----| -----
selectName|cssselector ordomobject |Note:
Returns an array of the object's class names
removeClassRemove one or more class names from a
Domobject.parameter name | description | default value ------| ----| -----
selectName|cssselector ordomobject |nameList| Class name to be deleted |Note:
nameListcan be passed as an array if you want to delete multiple class names.nameListcan be passed as a string if you want to delete a single class name.
setStyleSetting the style of a
Domobjectparameter name | description | default value ------| ----| -----
selectName|cssselector ordomobject |style| The style to be set |Note:
stylecan be passed as an object when you want to set multiple styles.stylecan be passed as a string when you want to set a single style.
Example:
import {setStyle} from 'femcjs'; setStyle('#dom', {color: 'red', backgroundColor: '#000'}); // set multiple styles setStyle('#dom', 'color: red'); // set a single stylegetDomInfoGet information about a
dom, or aNodeList.parameter name | description | default value ------| ----| -----
selectName| Acssselector or adomobject, or a set ofNodeLists.Note:
- If
selectNameis a set ofNodeLists, return an array of information consisting of theNodeLists.
Example:
import {setStyle} from 'femcjs'; console.log(getDomInfo($('li'))); // [DOMRect, DOMRect]- If
isExceedParentHeightDetermine if a child element exceeds the height of the parent element
parameter name | description | default value ------| ----| -----
parentSelectName| Parent element class name selector ordomobject |childSelectName| Child element class name selector ordomobject |Note:
The parent element has to set the height
isShowOnVisualAreaDetermine if an element appears in the visual area
parameter name | description | default value ------| ----| -----
selectName| Element class name selector ordomobject |setPageNoScrollSetting the page not to scroll
restorePageScrollResume page scrolling
Format
formatFileSizeFormatting File Size
parameter name | description | default value ------| ----| -----
file| file object |{}Example:
import {formatFileSize} from 'femcjs'; console.log(formatFileSize(file)); // 1.37KBformatVideoDurationFormatted Video Duration
parameter name | description | default value ------| ----| -----
totalSeconds| video duration |0Example:
import {formatVideoDuration} from 'femcjs'; console.log(formatVideoDuration(3600)); // 01:00:00
File
getFileNameGet the name of the
fileuploadedparameter name | description | default value ------| ----| -----
file|fileobjects |getFileSuffixGet the suffix of the uploaded
filefileparameter name | description | default value ------| ----| -----
file|fileobjects |getFilePreviewSrcGet the address of the uploaded
filepreviewparameter name | description | default value ------| ----| -----
file|fileobjects |Note:
- Mainly used for previewing uploaded images
- Note the use of the
revokeObjectURLmethod to reclaim memory.
Example:
import {getFilePreviewSrc, $} from 'femcjs'; const previewSrc = getFilePreviewSrc(file); $('#preview_img').src = previewSrc;isFileAudioTypeIs the uploaded file an audio type
parameter name | description | default value ------| ----| -----
file|fileobjects |isFilePicTypeIs the uploaded file an image type
parameter name | description | default value ------| ----| -----
file|fileobjects |isFileVideoTypeIs the uploaded file an video type
parameter name | description | default value ------| ----| -----
file|fileobjects |isContentSrtFormatIs the content format an srt file format
parameter name | description | default value ------| ----| -----
textStr| Contents text |
Object
emptyObjWhether the current object has no value
isArrWhether the current type is
arrayor notisDateWhether the current type is
Dateor notisFnWhether the current type is
functionor notisNullWhether the current is
nullor notisNumWhether the current type is
numberor notisObjWhether the current type is
Objector notisStrWhether the current type is
Stringor notisUndefinedWhether the current type is
Undefinedor notisHtmlObjWhether the current object is an
htmlDomobject.isNodeListWhether the current object is
NodeListor not.isFormDataWhether the current object is
FormDataor not.
Array
chunkArrBySizeReturns an array grouped according to a specified size.
parameter name | description | default value ------| ----| -----
array| array being manipulated |size| Size to specify |Example:
import {chunkArrBySize} from 'femcjs'; const arr = [1, 2, 3, 4, 5, 6, 7, 8]; const SIZE = 3; console.log(chunkArrBySize(arr, SIZE)); // [[1,2,3], [4,5,6], [7,8]]removeArrayItemDeleting an item in an array
parameter name | description | default value ------| ----| -----
array| array being manipulated |item| Items to be deleted |newone| Whether to return a new array without changing the size of the original array |falseNote:
itemcan only be of type string or numeric.- if
newone = false; changes the size of the original array - if
newone = true; does not change the size of the original array, returns a new target array
Example:
import {removeArrayItem} from 'femcjs'; const arr = [1, 2, 3, 4, 'hello', 'lee', 7, 8]; console.log(removeArrayItem(arr, 'hello', true)); // [1, 2, 3, 4, 'lee', 7, 8]; console.log(arr); // [1, 2, 3, 4, 'hello', 'lee', 7, 8];
Storage
getLocalStorageGetting a value from local storage
parameter name | description | default value ------| ----| -----
key| The key value to get |Note:
- if the
keyvalue is an object, return the object corresponding to thekeyvalue directly - otherwise, return the string corresponding to the
keyvalue.
Example:
// If localStorage stores info: {“name”: “lee”, “age”:29}, name: test import {getLocalStorage} from 'femcjs'; console.log(getLocalStorage(info)); // {name: lee, age} objects console.log(getLocalStorage(name)); // test string- if the
setLocalStorageSetting a LocalStorage value
parameter name | description | default value ------| ----| -----
key| The value of thekeyto be set |val| The value corresponding to thekeyto be set |Note:
valcan be an object or a stringExample:
import {setLocalStorage} from 'femcjs'; setLocalStorage('info', {name: 'lee', age: 29}); // info -> {"name":"lee","age":29}removeLocalStorageDelete one or more locally stored values
parameter name | description | default value ------| ----| -----
key| value ofkeyto be deleted |Note:
- if
keyis an array, multiple correspondingkeyvalues can be removed - if
keyis a string, then a single value is removed
Example:
import {removeLocalStorage} from 'femcjs'; console.log(removeLocalStorage(['info', 'test'])); // The values of info,test will be deleted.- if
clearLocalStorageClear Local Storage
Example:
import {clearLocalStorage} from 'femcjs'; clearLocalStorage();
validate
containCNDoes it contain Chinese
parameter name | description | default value ------| ----| -----
textStr| Contents text |Note:
Returns
truewhenever there is a Chinese character.containENDoes it contain English
parameter name | description | default value ------| ----| -----
textStr| Contents text |Note:
Returns
truewhenever there is English.containCNSpecialChar
Whether to include Chinese special characters
parameter name | description | default value ------| ----| -----
textStr| Contents text |Note:
Returns
truewhenever there are Chinese special characters.containENSpecialCharWhether to include English special characters
parameter name | description | default value ------| ----| -----
textStr| Contents text |Note:
Returns
truewhenever there is an English special character.isAllCNIs it all in Chinese?
parameter name | description | default value ------| ----| -----
textStr| Contents text |Note:
Is it in pure Chinese (Contains Chinese special characters and numbers; Excluding English and special English characters)
isAllENIs it all in English?
parameter name | description | default value ------| ----| -----
textStr| Contents text |Note:
Is it in pure English (Contains English special characters and numbers; Excluding English and special English characters)
isEmailIs it a mail format?
parameter name | description | default value ------| ----| -----
email| email |isPhoneNumIs it a cell phone number format
parameter name | description | default value ------| ----| -----
phoneNumber| mobile telephone number |Note:
Verify Mainland China Cell Phone Number
isIntlPhoneIs it an international cell phone number format
parameter name | description | default value ------| ----| -----
phoneNumber| international cell phone number |
Url
getUrlValueGet the value of the
urlparameterparameter name | description | default value ------| ----| -----
key| The key of the value to get |''url| Theurllink to get |window.location.hrefNote:
- When
keyis empty, an object consisting of the values of all the arguments ofurlis returned. - When
keyis a value, the value ofurlaskeyis returned.
Example:
import {getUrlValue} from 'femcjs'; const url = 'http://localhost:5501/test/?name=lee&age=29' console.log(getUrlValue('', url)); // {name: 'lmc', age: 29} console.log(getUrlValue('name', url)); // 'lmc'- When
setUrlWithNorefreshSetting the url and not refreshing the page
parameter name | description | default value ------| ----| -----
key| Value to be set |{}Example:
import {setUrlWithNorefresh} from 'femcjs'; console.log(setUrlWithNorefresh({name: 'lmc', age: 29}); // http://localhost:5501/test/?name=lmc&age=29
Other
getRandomIntGenerate random integers between [min, max]
parameter name | description | default value ------| ----| -----
min| minimum value |max| maximum value |Example:
import {getRandomInt} from 'femcjs'; //Example: Generate a random integer between 1 and 10 const randomNum = getRandomInt(1, 10); console.log(randomNum); // Possible outputs are 1, 2 .. Any number in 10getRemByPxConvert px to rem units
parameter name | description | default value ------| ----| -----
px|pxvalue |rootValue| The benchmark to be converted (if the design draft is 750, transmit 750) |1920unit| Return the converted unit string |trueExample:
import {getRemByPx} from 'femcjs'; // Based on the benchmark of 1920, obtain the converted rem of 100px const result = getRemByPx(100, 1920); console.log(result); // 10rem
Animation
vueanimationSupported animations are
fade-in: fade-infade-right-to-left: fade in from right to leftfade-top-to-bottom: fade in from top to bottomfade-zoom-in: zoom in
Example:
<Transition name="fade-right-to-left"> <components v-if="show" /> </Transition>cssanimationNote:
See
dist/styles/css-ani.cssfor details.Example:
<!-- The element will be delayed 500ms to 750ms for a cyclic blinking animation --> <div class="shine time750 delay500"></div>
