1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-15 21:18:02 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
nick-w-nick
b23ace0933 Merge 295fe153ad into 02086e7115 2024-10-08 10:03:06 -04:00
jbranchaud
02086e7115 Add Link A Scalar To An Array as a Zsh TIL 2024-10-07 18:51:57 -05:00
jbranchaud
0ecc41bd29 Add Add To The Path Via Path Array as a Zsh TIL 2024-10-07 18:37:23 -05:00
nick-w-nick
295fe153ad added mention of ES6 compatibility
Hello, I've added a small blockquote below the description to indicate that this method of accessing an indefinite number of function arguments has been superseded by the use of the spread operator via rest parameters for ES6+ compatibility.
2022-01-06 11:39:04 -05:00
4 changed files with 72 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1453 TILs and counting..._
_1455 TILs and counting..._
---
@@ -80,6 +80,7 @@ _1453 TILs and counting..._
* [XState](#xstate)
* [YAML](#yaml)
* [Zod](#zod)
* [Zsh](#zsh)
---
@@ -1726,6 +1727,11 @@ _1453 TILs and counting..._
- [Incorporate Existing Type Into Zod Schema](zod/incorporate-existing-type-into-zod-schema.md)
- [Set Custom Error Message For Nonempty Array](zod/set-custom-error-message-for-nonempty-array.md)
### Zsh
- [Add To The Path Via Path Array](zsh/add-to-the-path-via-path-array.md)
- [Link A Scalar To An Array](zsh/link-a-scalar-to-an-array.md)
## Usage
The `.vimrc` file for this project contains a function `CountTILs` that can

View File

@@ -5,6 +5,8 @@ an array-like object with all of the arguments to the function. Even if not
all of the arguments are referenced in the function signature, they can
still be accessed via the `arguments` object.
> For ES6+ compatibility, the `spread` operator used via [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) is preferred over the `arugments` object when accessing an abritrary number of function arguments.
```javascript
function argTest(one) {
console.log(one);

View File

@@ -0,0 +1,25 @@
# Add To Path Via Path Array
Typically when managing what is on your path in a Unix shell environment, you
override the `PATH` environment variable with `export`. This is usually an
append or prepend to bring along the existing path entries.
```bash
$ export PATH="$PATH:/Users/me/.local/bin"
```
The `zsh` shell environment exposes another way of adding to your path. They
have a `path` array which can be a little easier to work with since you can use
an array operation instead of string interpolation.
Here is how we'd do the same as above:
```bash
$ path+=/Users/me/.local/bin
```
This works because there is an automatic linking in zsh between arrays and
colon-separated strings (_scalars_).
[source](https://www.zsh.org/mla/users//2005/msg01132.html)
[source](https://superuser.com/a/1447959)

View File

@@ -0,0 +1,38 @@
# Link A Scalar To An Array
`Zsh` has a builtin command `typeset` that does a variety of things. When given
the `-T` flag and the names of a scalar and an array, it will link them
together so that a change to one is reflected in the other.
The scalar is a string of values delimited by a colon (`:`). The array is an
array that can be interacted with using array operations like append (`+=`).
```bash
$ typeset -T FOO foo
$ echo $FOO
$ export FOO="one:two"
$ echo $foo
one two
$ foo+=three
$ echo $FOO
one:two:three
```
Notice `FOO` is initially empty. I then `export` it to overwrite it with two
values delimited by a colon. Since `foo` is automatically kept in sync, I can
`echo $foo` and see those values displayed as an array. I can then append a
third value using an array operation on `foo`. The update will be automatically
reflected in `FOO`.
`Zsh` does this under the hood for `PATH` and `path` which is why you can [add
to the path via the path array](add-to-the-path-via-path-array.md).
See `man zshbuiltins` for more details.
[source](http://devlib.symbian.slions.net/s3/GUID-D87C96CE-3F23-552D-927C-B6A1D61691BF.html)