Table of Contents
Installation and Usage
- Print Control - SpiritWeb
- Label Design - Designer
- Label Printing - SpiritLabel
- Registration Code Authorization
- Membership Authorization
- embeded Javascript
Development Manual
- JS API
- Dynamic Link Library
- REST API
- Third-party Website Integration
- Android Native SDK
- H5 APP
FAQ
PrintSpirit JS Script Standard Library Function Manual
Overview
PrintSpirit JS script is a series of commonly used utility functions in the label design center, covering text processing, numerical processing, and date and time processing, aiming to improve development efficiency and code readability.
Table of Contents
- Text Processing
textValue()toUpper()toLower()desensitize()extractText()
- Numerical Processing
toPositiveInteger()toDecimal()roundDecimal()isInRange()toPercentage()padStart()padEnd()padBoth()toCustomRadix()toChineseUppercase()toChineseLowercase()toEnglishUppercase()toEnglishLowercase()
- Date and Time Processing
formatDate()formatDateTime()formatTime()diffDays()dateToChineseUpper()dateToChineseLower()dateToEnglishUpper()dateToEnglishLower()
- Others
getSerial()
好的,这是您提供的 JavaScript 函数的英文手册翻译。
JavaScript Functions Manual
Text Processing
textValue(text)
- Function: Retrieves the raw text value. This function returns the input text directly without any modification.
- Parameters:
text(String): The text to be processed.
- Returns:
- (String): The original text.
- Example:
1console.log(textValue("Hello World")); // Output: Hello World
toUpper(text)
- Function: Converts the text to uppercase.
- Parameters:
text(String): The text to be converted.
- Returns:
- (String): The converted uppercase text.
- Example:
1console.log(toUpper("hello world")); // Output: HELLO WORLD
toLower(text)
- Function: Converts the text to lowercase.
- Parameters:
text(String): The text to be converted.
- Returns:
- (String): The converted lowercase text.
- Example:
1console.log(toLower("HELLO WORLD")); // Output: hello world
desensitize(text, prefixLen = 3, suffixLen = 4, maskChar = '*')
- Function: Desensitizes text, commonly used to hide sensitive information (e.g., phone numbers, ID numbers). It preserves the beginning and end of the text and replaces the middle part with a specified character.
- Parameters:
text(String): The text to be desensitized.prefixLen(Number, Optional, Default:3): The number of characters to retain at the beginning of the text.suffixLen(Number, Optional, Default:4): The number of characters to retain at the end of the text.maskChar(String, Optional, Default:'*'):The character used to mask the middle part.
- Returns:
- (String): The desensitized text. If the text length is less than or equal to
prefixLen + suffixLen, the original text is returned.
- (String): The desensitized text. If the text length is less than or equal to
- Example:
1console.log(desensitize("13812345678")); // Output: 138****5678 2console.log(desensitize("abcdefg", 2, 2, '-')); // Output: ab---fg 3console.log(desensitize("short", 3, 4)); // Output: short (length insufficient for desensitization)
extractText(text, start, end)
- Function: Extracts a substring from the text.
- Parameters:
text(String): The original text.start(Number): The starting index for extraction (inclusive).end(Number): The ending index for extraction (exclusive).
- Returns:
- (String): The extracted substring.
- Example:
1console.log(extractText("Hello World", 0, 5)); // Output: Hello 2console.log(extractText("JavaScript", 4, 10)); // Output: Script
Numerical Processing
toPositiveInteger(value)
- Function: Converts a value to a positive integer. Returns
NaNif the conversion fails or the result is not a positive integer. - Parameters:
value(Any): The value to be converted.
- Returns:
- (Number): The converted positive integer or
NaN.
- (Number): The converted positive integer or
- Example:
1console.log(toPositiveInteger("123")); // Output: 123 2console.log(toPositiveInteger(45.67)); // Output: NaN 3console.log(toPositiveInteger("-10")); // Output: NaN 4console.log(toPositiveInteger("abc")); // Output: NaN
toDecimal(value)
- Function: Converts a value to a decimal number (float).
- Parameters:
value(Any): The value to be converted.
- Returns:
- (Number): The converted decimal number.
- Example:
1console.log(toDecimal("123.45")); // Output: 123.45 2console.log(toDecimal(100)); // Output: 100 3console.log(toDecimal("abc")); // Output: NaN
roundDecimal(value, decimalPlaces = 2)
- Function: Rounds a decimal number to the specified number of decimal places.
- Parameters:
value(Number): The number to be rounded.decimalPlaces(Number, Optional, Default:2): The number of decimal places to keep.
- Returns:
- (Number): The rounded number.
- Example:
1console.log(roundDecimal(123.4567)); // Output: 123.46 2console.log(roundDecimal(123.4, 0)); // Output: 123 3console.log(roundDecimal(123.454, 2)); // Output: 123.45
isInRange(value, min, max)
- Function: Checks if a number is within a specified range (inclusive of min and max).
- Parameters:
value(Number): The number to be checked.min(Number): The minimum value of the range.max(Number): The maximum value of the range.
- Returns:
- (Boolean):
trueif the value is within the range,falseotherwise.
- (Boolean):
- Example:
1console.log(isInRange(5, 0, 10)); // Output: true 2console.log(isInRange(0, 0, 10)); // Output: true 3console.log(isInRange(10, 0, 10)); // Output: true 4console.log(isInRange(-2, 0, 10)); // Output: false
toPercentage(value, decimalPlaces = 2)
- Function: Converts a number to a percentage string.
- Parameters:
value(Number): The number to be converted (e.g., 0.5 for 50%).decimalPlaces(Number, Optional, Default:2): The number of decimal places for the percentage.
- Returns:
- (String): The converted percentage string.
- Example:
1console.log(toPercentage(0.25)); // Output: 25.00% 2console.log(toPercentage(0.78912, 1)); // Output: 78.9% 3console.log(toPercentage(1)); // Output: 100.00%
padStart(value, totalLength, padChar = '0')
- Function: Pads the left side of a value with a specified character until it reaches the target total length.
- Parameters:
value(Any): The value to be padded.totalLength(Number): The target total length.padChar(String, Optional, Default:'0'): The character used for padding.
- Returns:
- (String): The padded string.
- Example:
1console.log(padStart(123, 5)); // Output: 00123 2console.log(padStart("abc", 6, '-')); // Output: ---abc 3console.log(padStart(12345, 3)); // Output: 12345 (already exceeds target length, no padding)
padEnd(value, totalLength, padChar = '0')
- Function: Pads the right side of a value with a specified character until it reaches the target total length.
- Parameters:
value(Any): The value to be padded.totalLength(Number): The target total length.padChar(String, Optional, Default:'0'): The character used for padding.
- Returns:
- (String): The padded string.
- Example:
1console.log(padEnd(123, 5)); // Output: 12300 2console.log(padEnd("abc", 6, '-')); // Output: abc--- 3console.log(padEnd(12345, 3)); // Output: 12345 (already exceeds target length, no padding)
padBoth(value, totalLength, padChar = '0')
- Function: Pads both sides of a value with a specified character until it reaches the target total length. If uneven padding is required, the left side will have one less pad character.
- Parameters:
value(Any): The value to be padded.totalLength(Number): The target total length.padChar(String, Optional, Default:'0'): The character used for padding.
- Returns:
- (String): The padded string.
- Example:
1console.log(padBoth(123, 7)); // Output: 0012300 2console.log(padBoth("abc", 7, '-')); // Output: --abc-- 3console.log(padBoth("test", 9, '*')); // Output: **test*** 4console.log(padBoth(12345, 3)); // Output: 12345 (already exceeds target length, no padding)
toCustomRadix(num, radix, charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
- Function: Converts a decimal number to its string representation in a custom radix (base). Supports negative numbers.
- Parameters:
num(Number): The decimal number to convert.radix(Number): The target base (must be between 2 and the length ofcharset).charset(String, Optional, Default:'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'): The set of characters used to represent digits.
- Returns:
- (String): The string representation in the custom radix.
- Throws:
Error: Ifradixis outside the valid range.
- Example:
1console.log(toCustomRadix(10, 2)); // Output: 1010 (binary) 2console.log(toCustomRadix(255, 16)); // Output: FF (hexadecimal) 3console.log(toCustomRadix(100, 36)); // Output: 2S (base 36, using default charset) 4console.log(toCustomRadix(-10, 2)); // Output: -1010 5console.log(toCustomRadix(10, 5, "abcde")); // Output: cde (using custom charset)
toChineseUppercase(num)
- Function: Converts a number or decimal to its Chinese uppercase monetary representation.
- Parameters:
num(Number): The number or decimal to convert.
- Returns:
- (String): The converted Chinese uppercase monetary string.
- Example:
1console.log(toChineseUppercase(123.45)); // Expected output: 壹佰贰拾叁点肆伍 (or 壹佰贰拾叁元肆角伍分) 2console.log(toChineseUppercase(10000)); // Expected output: 壹万元整 3console.log(toChineseUppercase(0)); // Expected output: 零元整
toChineseLowercase(num)
- Function: Converts a number or decimal to its Chinese lowercase representation. This function handles both integer and decimal parts, using "零, 一, 二..." and "点" to connect decimals.
- Parameters:
num(Number): The number or decimal to convert.
- Returns:
- (String): The converted Chinese lowercase string.
- Example:
1console.log(toChineseLowercase(123)); // Output: 一百二十三 2console.log(toChineseLowercase(101)); // Output: 一百零一 3console.log(toChineseLowercase(10)); // Output: 十 4console.log(toChineseLowercase(123.45)); // Output: 一百二十三点四五 5console.log(toChineseLowercase(0)); // Output: 零 6console.log(toChineseLowercase(-50)); // Output: 负五十 7console.log(toChineseLowercase(10000)); // Output: 一万 8console.log(toChineseLowercase(100000000)); // Output: 一亿 9console.log(toChineseLowercase(10005.02)); // Output: 一万零五点零二
toEnglishUppercase(num)
- Function: Converts a number or decimal to its English uppercase word representation.
- Parameters:
num(Number): The number or decimal to convert.
- Returns:
- (String): The converted English uppercase word string.
- Example:
1console.log(toEnglishUppercase(0)); // Output: ZERO 2console.log(toEnglishUppercase(123)); // Output: ONE HUNDRED AND TWENTY-THREE 3console.log(toEnglishUppercase(1000)); // Output: ONE THOUSAND 4console.log(toEnglishUppercase(1234567.89)); // Output: ONE MILLION TWO HUNDRED AND THIRTY-FOUR THOUSAND FIVE HUNDRED AND SIXTY-SEVEN POINT EIGHT NINE 5console.log(toEnglishUppercase(-42)); // Output: NEGATIVE FORTY-TWO 6console.log(toEnglishUppercase(15)); // Output: FIFTEEN
toEnglishLowercase(num)
- Function: Converts a number or decimal to its English lowercase word representation.
- Parameters:
num(Number): The number or decimal to convert.
- Returns:
- (String): The converted English lowercase word string.
- Example:
1console.log(toEnglishLowercase(0)); // Output: zero 2console.log(toEnglishLowercase(123)); // Output: one hundred and twenty-three 3console.log(toEnglishLowercase(1000)); // Output: one thousand 4console.log(toEnglishLowercase(1234567.89)); // Output: one million two hundred and thirty-four thousand five hundred and sixty-seven point eight nine 5console.log(toEnglishLowercase(-42)); // Output: negative forty-two
Date and Time Processing
formatDate(input, format = "YYYY-MM-DD")
- Function: Formats a Date object or a date string parseable into a Date object.
- Parameters:
input(Date | String | Any): A Date object, a date string, or any value parseable bynew Date(). If not provided ornull, defaults to the current date.format(String, Optional, Default:"YYYY-MM-DD"): The format string, supporting the following placeholders:YYYY: Four-digit year (e.g., 2023)YY: Two-digit year (e.g., 23)MM: Two-digit month (e.g., 01-12)M: One-digit month (e.g., 1-12)DD: Two-digit day (e.g., 01-31)D: One-digit day (e.g., 1-31)HH: Two-digit hours (24-hour format, e.g., 00-23)H: One-digit hours (24-hour format, e.g., 0-23)mm: Two-digit minutes (e.g., 00-59)m: One-digit minutes (e.g., 0-59)ss: Two-digit seconds (e.g., 00-59)s: One-digit seconds (e.g., 0-59)
- Returns:
- (String | null): The formatted date string. Returns
nullif the input is an invalid date.
- (String | null): The formatted date string. Returns
- Example:
1const now = new Date(); 2console.log(formatDate(now)); // Output: 2023-10-27 (assuming current date) 3console.log(formatDate("2023-01-05", "YY/M/D")); // Output: 23/1/5 4console.log(formatDate("2023-10-27 14:30:05", "YYYY年MM月DD日 HH时mm分ss秒")); 5// Output: 2023年10月27日 14时30分05秒 6console.log(formatDate("invalid date string")); // Output: null
formatDateTime(input)
- Function: Formats a date and time to
"YYYY-MM-DD HH:mm:ss"format. - Parameters:
input(Date | String | Any): Same as theinputparameter forformatDate.
- Returns:
- (String | null): The formatted date and time string.
- Example:
1const now = new Date(); 2console.log(formatDateTime(now)); // Output: 2023-10-27 14:30:05 (assuming current date and time) 3console.log(formatDateTime("2022-03-15T08:00:00")); // Output: 2022-03-15 08:00:00
formatTime(input)
- Function: Formats time to
"HH:mm:ss"format. - Parameters:
input(Date | String | Any): Same as theinputparameter forformatDate.
- Returns:
- (String | null): The formatted time string.
- Example:
1const now = new Date(); 2console.log(formatTime(now)); // Output: 14:30:05 (assuming current time) 3console.log(formatTime("2023-10-27 09:15:30")); // Output: 09:15:30
diffDays(date1, date2)
- Function: Calculates the absolute difference in days between two dates (rounded up).
- Parameters:
date1(Date | String | Any): The first date.date2(Date | String | Any): The second date.
- Returns:
- (Number | null): The difference in days (positive integer). Returns
nullif any date is invalid.
- (Number | null): The difference in days (positive integer). Returns
- Example:
1const d1 = new Date("2023-01-01"); 2const d2 = new Date("2023-01-03"); 3console.log(diffDays(d1, d2)); // Output: 2 4console.log(diffDays("2023-10-20", "2023-10-25")); // Output: 5 5console.log(diffDays("2023-10-25", "2023-10-20")); // Output: 5 (absolute value) 6console.log(diffDays("invalid", d2)); // Output: null
dateToChineseUpper(input)
- Function: Converts a date to its Chinese uppercase representation (year, month, day).
- Parameters:
input(Date | String | Any): The date to convert.
- Returns:
- (String): The converted Chinese uppercase date string. Returns
"无效日期"(Invalid Date) if the date is invalid.
- (String): The converted Chinese uppercase date string. Returns
- Example:
1console.log(dateToChineseUpper("2023-10-27")); // Output: 贰零贰叁年拾月贰柒日 2console.log(dateToChineseUpper("2023-01-05")); // Output: 贰零贰叁年壹月伍日 3console.log(dateToChineseUpper(new Date())); // Output: Current date in Chinese uppercase
dateToChineseLower(input)
- Function: Converts a date to its Chinese lowercase representation (year, month, day).
- Parameters:
input(Date | String | Any): The date to convert.
- Returns:
- (String): The converted Chinese lowercase date string. Returns
"无效日期"(Invalid Date) if the date is invalid.
- (String): The converted Chinese lowercase date string. Returns
- Example:
1console.log(dateToChineseLower("2023-10-27")); // Output: 二零二三年十月二十七日 2console.log(dateToChineseLower("2023-01-05")); // Output: 二零二三年一月五日 3console.log(dateToChineseLower(new Date())); // Output: Current date in Chinese lowercase
dateToEnglishUpper(input)
- Function: Converts a date to its English uppercase representation (e.g., OCTOBER 27, 2023).
- Parameters:
input(Date | String | Any): The date to convert.
- Returns:
- (String): The converted English uppercase date string. Returns
"INVALID DATE"if the date is invalid.
- (String): The converted English uppercase date string. Returns
- Example:
1console.log(dateToEnglishUpper("2023-10-27")); // Output: OCTOBER 27, 2023 2console.log(dateToEnglishUpper("2023-01-05")); // Output: JANUARY 5, 2023 3console.log(dateToEnglishUpper(new Date())); // Output: Current date in English uppercase
dateToEnglishLower(input)
- Function: Converts a date to its English lowercase representation (e.g., october 27, 2023).
- Parameters:
input(Date | String | Any): The date to convert.
- Returns:
- (String): The converted English lowercase date string. Returns
"invalid date"if the date is invalid.
- (String): The converted English lowercase date string. Returns
- Example:
1console.log(dateToEnglishLower("2023-10-27")); // Output: october 27, 2023 2console.log(dateToEnglishLower("2023-01-05")); // Output: january 5, 2023 3console.log(dateToEnglishLower(new Date())); // Output: Current date in English lowercase
Leave Your Message
- PrintSpirit JS Script Standard Library Function Manual
-
JavaScript Functions Manual
- Text Processing
-
Numerical Processing
- toPositiveInteger(value)
- toDecimal(value)
- roundDecimal(value, decimalPlaces = 2)
- isInRange(value, min, max)
- toPercentage(value, decimalPlaces = 2)
- padStart(value, totalLength, padChar = '0')
- padEnd(value, totalLength, padChar = '0')
- padBoth(value, totalLength, padChar = '0')
- toCustomRadix(num, radix, charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
- toChineseUppercase(num)
- toChineseLowercase(num)
- toEnglishUppercase(num)
- toEnglishLowercase(num)
- Date and Time Processing