30秒学会 JavaScript 片段 – untildify
Converts a tilde path to an absolute path.
Use String.prototype.replace()
with a regular expression and OS.homedir()
to replace the ~
in the start of the path with the home directory.
分享文章、钻研技术、改变生活!
Converts a tilde path to an absolute path.
Use String.prototype.replace()
with a regular expression and OS.homedir()
to replace the ~
in the start of the path with the home directory.
Creates a striped list with alternating background colors, which is useful for differentiating siblings that have content spread across a wide row.
:nth-child(odd)
or :nth-child(even)
pseudo-class to apply a different background color to elements that match based on their position in a group of siblings.div
, tr
, p
, ol
, etc.Renders a tooltip component.
React.useState()
hook to create the show
variable and initialize it to false
.<div>
element that contains the <div>
that will be the tooltip and the children
passed to the component.onMouseEnter
and onMouseLeave
methods, by altering the value of the show
variable.Splits a multiline string into a list of lines.
Use s.split()
and '\n'
to match line breaks and create a list.
str.splitlines()
provides similar functionality to this snippet.
Checks if two numbers are approximately equal to each other.
Use Math.abs()
to compare the absolute difference of the two values to epsilon
.
Omit the third parameter, epsilon
, to use a default value of 0.001
.
Returns true
if the given string is upper case, false otherwise.
Convert the given string to upper case, using strtoupper
and compare it to the original.
Vertically and horizontally centers a child element within its parent element using position: absolute
and transform: translate()
(as an alternative to flexbox
or display: table
).
Similar to flexbox
, this method does not require you to know the height or width of your parent or child so it is ideal for responsive applications.
position: absolute
on the child element allows it to be positioned based on its containing block.left: 50%
and top: 50%
offsets the child 50% from the left and top edge of its containing block.transform: translate(-50%, -50%)
allows the height and width of the child element to be negated so that it is vertically and horizontally centered.Returns the last element for which the provided function returns a truthy value.
Use Array.prototype.filter()
to remove elements for which fn
returns falsy values, Array.prototype.pop()
to get the last one.
Returns yesterday’s DateTime
value.
Use DateTime.Now
to get the current date, then use DateTime.AddDays(-1)
to decrement by 1
.
Returns the length of a string in bytes.
Convert a given string to a Blob
Object and find its size
.