Skip to content

Commit f964c18

Browse files
authored
Initial code (#1)
* Initial commit of code * Fleshing out tests * Removing passing 'result' recursively * Updating README.md, fixing UMD output * Adding '.github' directory
1 parent 3eace1b commit f964c18

22 files changed

+4504
-3
lines changed

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [avoidwork]

.github/dependabot.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: "/"
5+
schedule:
6+
interval: weekly
7+
open-pull-requests-limit: 10
8+
- package-ecosystem: npm
9+
directory: "/"
10+
schedule:
11+
interval: daily
12+
open-pull-requests-limit: 10

.github/workflows/ci.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
name: ci
3+
4+
on:
5+
pull_request:
6+
branches:
7+
- main
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [18.x, 20.x]
16+
17+
steps:
18+
- name: Checkout Repository
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 1
22+
23+
- name: Setup Node ${{ matrix.node-version }}
24+
uses: actions/setup-node@v4
25+
with:
26+
always-auth: false
27+
node-version: ${{ matrix.node-version }}
28+
29+
- name: Install dependencies
30+
run: npm install
31+
32+
- name: Build
33+
run: npm run rollup
34+
35+
- name: Run Tests
36+
run: npm run test
37+
38+
automerge:
39+
needs: build
40+
runs-on: ubuntu-latest
41+
permissions:
42+
pull-requests: write
43+
contents: write
44+
steps:
45+
- uses: fastify/github-action-merge-dependabot@v3
46+
with:
47+
github-token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ out
8989

9090
# Nuxt.js build / generate output
9191
.nuxt
92-
dist
9392

9493
# Gatsby files
9594
.cache/
@@ -128,3 +127,5 @@ dist
128127
.yarn/build-state.yml
129128
.yarn/install-state.gz
130129
.pnp.*
130+
131+
.idea

.husky/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npm test

README.md

+200-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,200 @@
1-
# tiny-strings
2-
String scraper
1+
# Tiny Strings
2+
3+
Scrape Strings from Arrays and Objects.
4+
5+
## API
6+
7+
### strings(*arg = {}, keys = false*)
8+
9+
Returns an `Array` of `Strings` extracted from `arg`
10+
11+
param {Array} or {Object} to scrape
12+
param {Boolean} to include Object keys
13+
return {Array} Array of Strings scraped from 'arg'
14+
15+
**Example**
16+
17+
Without keys
18+
19+
```javascript
20+
strings({...});
21+
```
22+
23+
With keys
24+
```javascript
25+
strings({...}, true);
26+
```
27+
28+
## Testing
29+
30+
Tiny Strings has 100% code coverage with its tests.
31+
32+
```console
33+
------------------|---------|----------|---------|---------|-------------------
34+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
35+
------------------|---------|----------|---------|---------|-------------------
36+
All files | 100 | 90.9 | 100 | 100 |
37+
tiny-strings.cjs | 100 | 90.9 | 100 | 100 | 14,25
38+
------------------|---------|----------|---------|---------|-------------------
39+
```
40+
41+
## Example
42+
43+
### Input
44+
45+
```javascript
46+
import {strings} from "tiny-strings";
47+
const extracted = strings([
48+
{
49+
"_id": "66897c0b5bb3f3bda598a7be",
50+
"index": 0,
51+
"guid": "57cc37b7-ea7b-4cf5-b07e-238a15cc1cdd",
52+
"isActive": true,
53+
"balance": "$1,290.80",
54+
"picture": "http://placehold.it/32x32",
55+
"age": 21,
56+
"eyeColor": "blue",
57+
"name": "Valeria Jenkins",
58+
"gender": "female",
59+
"company": "GEOLOGIX",
60+
"email": "[email protected]",
61+
"phone": "+1 (976) 597-3338",
62+
"address": "681 Jackson Place, Collins, Nevada, 8477",
63+
"about": "Officia sunt fugiat aute eiusmod cillum excepteur enim consectetur in pariatur consequat cupidatat. Id minim anim sint cupidatat nostrud labore esse proident eiusmod eu. Dolor sit aute adipisicing est proident eiusmod voluptate reprehenderit sunt aliquip non quis. Lorem voluptate laboris aliquip cupidatat.\r\n",
64+
"registered": "2016-04-05T01:14:10 +04:00",
65+
"latitude": 8.514373,
66+
"longitude": -22.578972,
67+
"tags": [
68+
"voluptate",
69+
"esse",
70+
"amet",
71+
"mollit",
72+
"in",
73+
"consectetur",
74+
"sunt"
75+
],
76+
"friends": [
77+
{
78+
"id": 0,
79+
"name": "Moore Howell"
80+
},
81+
{
82+
"id": 1,
83+
"name": "Summer Cline"
84+
},
85+
{
86+
"id": 2,
87+
"name": "Francis Simon"
88+
}
89+
],
90+
"greeting": "Hello, Valeria Jenkins! You have 1 unread messages.",
91+
"favoriteFruit": "banana"
92+
},
93+
{
94+
"_id": "66897c0b0eea9250887803d6",
95+
"index": 1,
96+
"guid": "6eac3134-b442-4289-a498-ef9b5624f196",
97+
"isActive": false,
98+
"balance": "$1,302.96",
99+
"picture": "http://placehold.it/32x32",
100+
"age": 28,
101+
"eyeColor": "brown",
102+
"name": "Amanda Rodgers",
103+
"gender": "female",
104+
"company": "GRAINSPOT",
105+
"email": "[email protected]",
106+
"phone": "+1 (987) 457-3247",
107+
"address": "650 Osborn Street, Edenburg, New Jersey, 1377",
108+
"about": "Eu minim enim aliquip nulla aliquip elit sit nostrud dolore reprehenderit aliquip sit qui Lorem. Enim et excepteur dolor velit minim nulla ullamco nisi. Qui adipisicing id occaecat nisi duis quis sint minim laborum minim id. Do id aliqua deserunt incididunt nulla officia nostrud reprehenderit laborum aliquip qui. Veniam pariatur eu quis reprehenderit velit nostrud deserunt deserunt. Commodo duis esse deserunt laborum tempor dolore anim laborum deserunt ipsum sit tempor consectetur.\r\n",
109+
"registered": "2023-05-27T09:39:08 +04:00",
110+
"latitude": 45.890773,
111+
"longitude": -29.059383,
112+
"tags": [
113+
"Lorem",
114+
"sunt",
115+
"cillum",
116+
"ea",
117+
"in",
118+
"aute",
119+
"culpa"
120+
],
121+
"friends": [
122+
{
123+
"id": 0,
124+
"name": "Jo Nunez"
125+
},
126+
{
127+
"id": 1,
128+
"name": "Mcdonald Burns"
129+
},
130+
{
131+
"id": 2,
132+
"name": "Pacheco Vang"
133+
}
134+
],
135+
"greeting": "Hello, Amanda Rodgers! You have 7 unread messages.",
136+
"favoriteFruit": "strawberry"
137+
}
138+
]);
139+
```
140+
141+
### Output
142+
143+
```javascript
144+
[
145+
"66897c0b5bb3f3bda598a7be",
146+
"57cc37b7-ea7b-4cf5-b07e-238a15cc1cdd",
147+
"$1,290.80",
148+
"http://placehold.it/32x32",
149+
"blue",
150+
"Valeria Jenkins",
151+
"female",
152+
"GEOLOGIX",
153+
154+
"+1 (976) 597-3338",
155+
"681 Jackson Place, Collins, Nevada, 8477",
156+
"Officia sunt fugiat aute eiusmod cillum excepteur enim consectetur in pariatur consequat cupidatat. Id minim anim sint cupidatat nostrud labore esse proident eiusmod eu. Dolor sit aute adipisicing est proident eiusmod voluptate reprehenderit sunt aliquip non quis. Lorem voluptate laboris aliquip cupidatat.\r\n",
157+
"2016-04-05T01:14:10 +04:00",
158+
"voluptate",
159+
"esse",
160+
"amet",
161+
"mollit",
162+
"in",
163+
"consectetur",
164+
"sunt",
165+
"Moore Howell",
166+
"Summer Cline",
167+
"Francis Simon",
168+
"Hello, Valeria Jenkins! You have 1 unread messages.",
169+
"banana",
170+
"66897c0b0eea9250887803d6",
171+
"6eac3134-b442-4289-a498-ef9b5624f196",
172+
"$1,302.96",
173+
"http://placehold.it/32x32",
174+
"brown",
175+
"Amanda Rodgers",
176+
"female",
177+
"GRAINSPOT",
178+
179+
"+1 (987) 457-3247",
180+
"650 Osborn Street, Edenburg, New Jersey, 1377",
181+
"Eu minim enim aliquip nulla aliquip elit sit nostrud dolore reprehenderit aliquip sit qui Lorem. Enim et excepteur dolor velit minim nulla ullamco nisi. Qui adipisicing id occaecat nisi duis quis sint minim laborum minim id. Do id aliqua deserunt incididunt nulla officia nostrud reprehenderit laborum aliquip qui. Veniam pariatur eu quis reprehenderit velit nostrud deserunt deserunt. Commodo duis esse deserunt laborum tempor dolore anim laborum deserunt ipsum sit tempor consectetur.\r\n",
182+
"2023-05-27T09:39:08 +04:00",
183+
"Lorem",
184+
"sunt",
185+
"cillum",
186+
"ea",
187+
"in",
188+
"aute",
189+
"culpa",
190+
"Jo Nunez",
191+
"Mcdonald Burns",
192+
"Pacheco Vang",
193+
"Hello, Amanda Rodgers! You have 7 unread messages.",
194+
"strawberry"
195+
]
196+
```
197+
198+
## License
199+
Copyright (c) 2024 Jason Mulligan
200+
Licensed under the BSD-3 license.

dist/tiny-strings.cjs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* tiny-strings
3+
*
4+
* @copyright 2024 Jason Mulligan <[email protected]>
5+
* @license BSD-3-Clause
6+
* @version 1.0.0
7+
*/
8+
'use strict';
9+
10+
const STRING_STRING = "string";
11+
const STRING_OBJECT = "object";
12+
const MSG_INVALID_INPUT = "Argument must be an Array or Object";
13+
14+
function strings (arg = {}, keys = false) {
15+
if (typeof arg !== STRING_OBJECT) {
16+
throw new TypeError(MSG_INVALID_INPUT);
17+
}
18+
19+
const result = [];
20+
21+
if (Array.isArray(arg)) {
22+
for (const item of arg) {
23+
if (typeof item === STRING_STRING) {
24+
result.push(item);
25+
} else if (typeof item === STRING_OBJECT) {
26+
result.push(...strings(item, keys));
27+
}
28+
}
29+
} else {
30+
const argKeys = Object.keys(arg);
31+
32+
if (keys) {
33+
result.push(...argKeys);
34+
}
35+
36+
for (const key of argKeys) {
37+
if (typeof arg[key] === STRING_STRING) {
38+
result.push(arg[key]);
39+
} else if (Array.isArray(arg[key])) {
40+
for (const value of arg[key]) {
41+
if (typeof value === STRING_OBJECT) {
42+
result.push(...strings(value, keys));
43+
} else if (typeof value === STRING_STRING) {
44+
result.push(value);
45+
}
46+
}
47+
} else if (typeof arg[key] === STRING_OBJECT) {
48+
result.push(...strings(arg[key], keys));
49+
}
50+
}
51+
}
52+
53+
return result;
54+
}
55+
56+
exports.strings = strings;

dist/tiny-strings.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* tiny-strings
3+
*
4+
* @copyright 2024 Jason Mulligan <[email protected]>
5+
* @license BSD-3-Clause
6+
* @version 1.0.0
7+
*/
8+
const STRING_STRING = "string";
9+
const STRING_OBJECT = "object";
10+
const MSG_INVALID_INPUT = "Argument must be an Array or Object";function strings (arg = {}, keys = false) {
11+
if (typeof arg !== STRING_OBJECT) {
12+
throw new TypeError(MSG_INVALID_INPUT);
13+
}
14+
15+
const result = [];
16+
17+
if (Array.isArray(arg)) {
18+
for (const item of arg) {
19+
if (typeof item === STRING_STRING) {
20+
result.push(item);
21+
} else if (typeof item === STRING_OBJECT) {
22+
result.push(...strings(item, keys));
23+
}
24+
}
25+
} else {
26+
const argKeys = Object.keys(arg);
27+
28+
if (keys) {
29+
result.push(...argKeys);
30+
}
31+
32+
for (const key of argKeys) {
33+
if (typeof arg[key] === STRING_STRING) {
34+
result.push(arg[key]);
35+
} else if (Array.isArray(arg[key])) {
36+
for (const value of arg[key]) {
37+
if (typeof value === STRING_OBJECT) {
38+
result.push(...strings(value, keys));
39+
} else if (typeof value === STRING_STRING) {
40+
result.push(value);
41+
}
42+
}
43+
} else if (typeof arg[key] === STRING_OBJECT) {
44+
result.push(...strings(arg[key], keys));
45+
}
46+
}
47+
}
48+
49+
return result;
50+
}export{strings};

dist/tiny-strings.min.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*!
2+
2024 Jason Mulligan <[email protected]>
3+
@version 1.0.0
4+
*/
5+
const o="string",e="object";function r(t={},s=!1){if(typeof t!==e)throw new TypeError("Argument must be an Array or Object");const f=[];if(Array.isArray(t))for(const p of t)typeof p===o?f.push(p):typeof p===e&&f.push(...r(p,s));else{const p=Object.keys(t);s&&f.push(...p);for(const y of p)if(typeof t[y]===o)f.push(t[y]);else if(Array.isArray(t[y]))for(const p of t[y])typeof p===e?f.push(...r(p,s)):typeof p===o&&f.push(p);else typeof t[y]===e&&f.push(...r(t[y],s))}return f}export{r as strings};//# sourceMappingURL=tiny-strings.min.js.map

0 commit comments

Comments
 (0)