Skip to content

Commit

Permalink
fix type association and credentials requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo authored and Leo committed Nov 23, 2016
1 parent 17ca3df commit 8525f22
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 58 deletions.
47 changes: 31 additions & 16 deletions CSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,48 @@ module.exports = {
var data = [], keys = [], header = true
var i = 0, start = 0, col = 0, item = {}
while (i < csv.length) {
if (i === "\"") {
if (csv[i] === "\"") {
start = i + 1
while (++i) {
if (i === "\\") i += 2
if (i === "\"") break
if (csv[i] === "\\") i += 2
if (csv[i] === "\"") break
}
var value = csv.substring(start, i).replace(/(\\.)/g, "$1")
var value = csv.substring(start, i).replace(/\\(.)/g, "$1")
if (header) keys.push(value)
else item[keys[col]] = value
start = ++i
i++
continue
}
if (i === ",") {
if (csv[i] === ",") {
var value = csv.substring(start, i)
if (header) keys.push(value)
else item[keys[col]] = value
else item[keys[col]] = item[keys[col]] != null ? item[keys[col]] : value
start = ++i
col++
continue
}
if (i === "\n") {
header = false
if (csv[i] === "\n") {
var value = csv.substring(start, i)
start = ++i
if (header) {
keys.push(value)
header = false
}
else {
item[keys[col]] = item[keys[col]] != null ? item[keys[col]] : value
data.push(item)
item = {}
}
col = 0
data.push(item)
item = {}
continue
}
i++
}
if (Object.keys(item).length > 0) data.push(item)
if (Object.keys(item).length > 0) {
var value = csv.substring(start, i)
item[keys[col]] = item[keys[col]] != null ? item[keys[col]] : value
data.push(item)
}
return data
},
create: function(list) {
Expand All @@ -39,10 +54,10 @@ module.exports = {
}
data = data.slice(0, -1) + "\n"

for (var i = 0; i < data.length; i++) {
for (var k in list[i]) {
var value = String(list[i][k])
data += value.indexOf("\"") > -1 ? "\"" + value.replace(/("|,)/g, "\\$1") + "\"" : value + ","
for (var i = 0; i < list.length; i++) {
for (var k in list[0]) {
var value = list[i][k] !== undefined ? String(list[i][k]) : ""
data += value.indexOf("\"") > -1 ? "\"" + value.replace(/("|,)/g, "\\$1") + "\"," : value + ","
}
data = data.slice(0, -1) + "\n"
}
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ A starting point for big dreams.

REM is a REST API for prototyping. It accepts JSON requests, returns JSON responses and persists data between requests like a real API. But your test data is only visible to you. It's [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) enabled and no API key is required.

```javascript
var xhr = new XMLHttpRequest()
xhr.open("GET", "http://rem-rest-api.herokuapp.com/api/users", true)
xhr.withCredentials = true
xhr.send()
xhr.onload = function() {
var data = JSON.parse(xhr.responseText)
}
```

---

### API
Expand Down Expand Up @@ -116,6 +126,6 @@ Note that cookies can only store about 4kb worth of data. If you go over the lim

### About

REM is ~200 LOC, and has no NPM dependencies.
REM is ~250 LOC, and has no NPM dependencies.

Licence: MIT
20 changes: 20 additions & 0 deletions Type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
encode: function(list) {
return list.map(function(item) {
var obj = {}
Object.keys(item).forEach(function(key) {
obj[key] = JSON.stringify(item[key])
})
return obj
})
},
decode: function(list) {
return list.map(function(item) {
var obj = {}
Object.keys(item).forEach(function(key) {
obj[key] = item[key] !== "" ? JSON.parse(item[key]) : undefined
})
return obj
})
},
}
50 changes: 25 additions & 25 deletions data.json
Original file line number Diff line number Diff line change
@@ -1,127 +1,127 @@
{
"users": [
{
"id": "1",
"id": 1,
"firstName": "Peter",
"lastName": "Mackenzie"
},
{
"id": "2",
"id": 2,
"firstName": "Cindy",
"lastName": "Zhang"
},
{
"id": "3",
"id": 3,
"firstName": "Ted",
"lastName": "Smith"
},
{
"id": "4",
"id": 4,
"firstName": "Susan",
"lastName": "Fernbrook"
},
{
"id": "5",
"id": 5,
"firstName": "Emily",
"lastName": "Kim"
},
{
"id": "6",
"id": 6,
"firstName": "Peter",
"lastName": "Zhang"
},
{
"id": "7",
"id": 7,
"firstName": "Cindy",
"lastName": "Smith"
},
{
"id": "8",
"id": 8,
"firstName": "Ted",
"lastName": "Fernbrook"
},
{
"id": "9",
"id": 9,
"firstName": "Susan",
"lastName": "Kim"
},
{
"id": "10",
"id": 10,
"firstName": "Emily",
"lastName": "Mackenzie"
},
{
"id": "11",
"id": 11,
"firstName": "Peter",
"lastName": "Smith"
},
{
"id": "12",
"id": 12,
"firstName": "Cindy",
"lastName": "Fernbrook"
},
{
"id": "13",
"id": 13,
"firstName": "Ted",
"lastName": "Kim"
},
{
"id": "14",
"id": 14,
"firstName": "Susan",
"lastName": "Mackenzie"
},
{
"id": "15",
"id": 15,
"firstName": "Emily",
"lastName": "Zhang"
},
{
"id": "16",
"id": 16,
"firstName": "Peter",
"lastName": "Fernbrook"
},
{
"id": "17",
"id": 17,
"firstName": "Cindy",
"lastName": "Kim"
},
{
"id": "18",
"id": 18,
"firstName": "Ted",
"lastName": "Mackenzie"
},
{
"id": "19",
"id": 19,
"firstName": "Susan",
"lastName": "Zhang"
},
{
"id": "20",
"id": 20,
"firstName": "Emily",
"lastName": "Smith"
},
{
"id": "21",
"id": 21,
"firstName": "Peter",
"lastName": "Kim"
},
{
"id": "22",
"id": 22,
"firstName": "Cindy",
"lastName": "Mackenzie"
},
{
"id": "23",
"id": 23,
"firstName": "Ted",
"lastName": "Zhang"
},
{
"id": "24",
"id": 24,
"firstName": "Susan",
"lastName": "Smith"
},
{
"id": "25",
"id": 25,
"firstName": "Emily",
"lastName": "Fernbrook"
}
Expand Down
10 changes: 9 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ <h1 id="rem-rest-api">REM REST API</h1>

<p>REM is a REST API for prototyping. It accepts JSON requests, returns JSON responses and persists data between requests like a real API. But your test data is only visible to you. It's <a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing">CORS</a> enabled and no API key is required.</p>

<pre><code>var xhr = new XMLHttpRequest()
xhr.open("GET", "http://rem-rest-api.herokuapp.com/api/users", true)
xhr.withCredentials = true
xhr.send()
xhr.onload = function() {
var data = JSON.parse(xhr.responseText)
}</code></pre>

<hr />

<h3 id="api">API</h3>
Expand Down Expand Up @@ -125,7 +133,7 @@ <h3 id="how-it-works">How it works</h3>

<h3 id="about">About</h3>

<p>REM is ~200 LOC, and has no NPM dependencies.</p>
<p>REM is ~250 LOC, and has no NPM dependencies.</p>

<p>Licence: MIT</p>
</main>
Expand Down
Loading

0 comments on commit 8525f22

Please sign in to comment.