Skip to content

Commit

Permalink
append prepend
Browse files Browse the repository at this point in the history
  • Loading branch information
litstat committed Oct 18, 2024
1 parent bee0131 commit 93ccb7e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_modules
node_modules
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Basis
Copyright (c) 2023-2024 Basis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,16 @@ round2(n)

#### array
```js
append(a = [])
append(a, add)
appendShallow(a, ...add)
array(a = [])
countBy(a, k)
fields(columns, ...add)
groupBy(a, k, v)
indexBy(a, k, v)
partition(a, fn)
prepend(a = [])
prepend(a, add)
prependShallow(a, ...add)
remove(a, v, all)
transform(a, fn, buf = [])
uniq(a)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "helpers",
"version": "0.1.1",
"version": "0.1.3",
"description": "shared javascript toolkit",
"type": "module",
"exports": {
Expand Down
17 changes: 15 additions & 2 deletions src/append.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { nativeIsArray } from 'underscore/modules/_setup.js';
import { array } from './array';

/**
* Append array to array
*/
export function append(a, add) {
for (var i = 0; i < add.length; i++) {
a.push(add[i]);
}

return a;
}

/**
* Append arguments to shallow array
*/
export function append(a) {
export function appendShallow(a) {
a = array(a);

for (var i = 1; i < arguments.length; i++) {
var item = arguments[i];

if (nativeIsArray(item)) {
append(a, ...item);
for (var j = 0; j < item.length; j++) {
appendShallow(a, item[j]);
}
}
else {
a.push(item);
Expand Down
4 changes: 2 additions & 2 deletions src/fields.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { append } from './append';
import { appendShallow } from './append';
import { uniq } from './uniq';

/**
Expand All @@ -8,7 +8,7 @@ export function fields(columns, ...add) {
var buf = [];

_fields(columns, buf);
append(buf, add);
appendShallow(buf, add);

return uniq(buf);
}
Expand Down
17 changes: 15 additions & 2 deletions src/prepend.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { nativeIsArray } from 'underscore/modules/_setup.js';
import { array } from './array';

/**
* Unshift array to array
*/
export function prepend(a, add) {
for (var i = add.length - 1; i >= 0; i--) {
a.unshift(add[i]);
}

return a;
}

/**
* Unshift arguments to shallow array
*/
export function prepend(a) {
export function prependShallow(a) {
a = array(a);

for (var i = arguments.length - 1; i > 0; i--) {
var item = arguments[i];

if (nativeIsArray(item)) {
prepend(a, ...item);
for (var j = item.length - 1; j >= 0; j--) {
prependShallow(a, item[j]);
}
}
else {
a.unshift(item);
Expand Down

0 comments on commit 93ccb7e

Please sign in to comment.